home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-03  |  105.1 KB  |  3,974 lines  |  [TEXT/MPS ]

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. This paragraph is here to try to keep Sun CC from dying.
  21. The number of chars here seems crucial!!!!  */
  22.  
  23. /* This program is the user interface to the C compiler and possibly to
  24. other compilers.  It is used because compilation is a complicated procedure
  25. which involves running several programs and passing temporary files between
  26. them, forwarding the users switches to those programs selectively,
  27. and deleting the temporary files at the end.
  28.  
  29. CC recognizes how to compile each input file by suffixes in the file names.
  30. Once it knows which kind of compilation to perform, the procedure for
  31. compilation is specified by a string called a "spec".  */
  32.  
  33. #ifdef MPW
  34. #else
  35. #include <sys/types.h>
  36. #include <ctype.h>
  37. #include <signal.h>
  38. #include <sys/stat.h>
  39. #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
  40. #endif
  41.  
  42. #include "config.h"
  43. #include "obstack.h"
  44. #include "gvarargs.h"
  45. #include <stdio.h>
  46.  
  47. #ifndef R_OK
  48. #define R_OK 4
  49. #define W_OK 2
  50. #define X_OK 1
  51. #endif
  52.  
  53. /* Define a generic NULL if one hasn't already been defined.  */
  54.  
  55. #ifndef NULL
  56. #define NULL 0
  57. #endif
  58.  
  59. #ifndef GENERIC_PTR
  60. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  61. #define GENERIC_PTR void *
  62. #else
  63. #define GENERIC_PTR char *
  64. #endif
  65. #endif
  66.  
  67. #ifndef NULL_PTR
  68. #define NULL_PTR ((GENERIC_PTR)0)
  69. #endif
  70.  
  71. #ifdef USG
  72. #define vfork fork
  73. #endif /* USG */
  74.  
  75. /* On MSDOS, write temp files in current dir
  76.    because there's no place else we can expect to use.  */
  77. #if __MSDOS__
  78. #ifndef P_tmpdir
  79. #define P_tmpdir "./"
  80. #endif
  81. #endif
  82.  
  83. /* Test if something is a normal file.  */
  84. #ifndef S_ISREG
  85. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  86. #endif
  87.  
  88. /* Test if something is a directory.  */
  89. #ifndef S_ISDIR
  90. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  91. #endif
  92.  
  93. /* By default there is no special suffix for executables.  */
  94. #ifndef EXECUTABLE_SUFFIX
  95. #define EXECUTABLE_SUFFIX ""
  96. #endif
  97.  
  98. /* By default, colon separates directories in a path.  */
  99. #ifndef PATH_SEPARATOR
  100. #define PATH_SEPARATOR ':'
  101. #endif
  102.  
  103. #define obstack_chunk_alloc xmalloc
  104. #define obstack_chunk_free free
  105.  
  106. extern void free ();
  107. extern char *getenv ();
  108.  
  109. extern int errno, sys_nerr;
  110. extern char *sys_errlist[];
  111.  
  112. extern int execv (), execvp ();
  113.  
  114. /* If a stage of compilation returns an exit status >= 1,
  115.    compilation of that file ceases.  */
  116.  
  117. #define MIN_FATAL_STATUS 1
  118.  
  119. /* Flag saying to print the full filename of libgcc.a
  120.    as found through our usual search mechanism.  */
  121.  
  122. static int print_libgcc_file_name;
  123.  
  124. /* Flag indicating whether we should print the command and arguments */
  125.  
  126. static int verbose_flag;
  127.  
  128. /* Nonzero means write "temp" files in source directory
  129.    and use the source file's name in them, and don't delete them.  */
  130.  
  131. static int save_temps_flag;
  132.  
  133. /* The compiler version specified with -V */
  134.  
  135. static char *spec_version;
  136.  
  137. /* The target machine specified with -b.  */
  138.  
  139. static char *spec_machine = DEFAULT_TARGET_MACHINE;
  140.  
  141. /* Nonzero if cross-compiling.
  142.    When -b is used, the value comes from the `specs' file.  */
  143.  
  144. #ifdef CROSS_COMPILE
  145. static int cross_compile = 1;
  146. #else
  147. static int cross_compile = 0;
  148. #endif
  149.  
  150. /* This is the obstack which we use to allocate many strings.  */
  151.  
  152. static struct obstack obstack;
  153.  
  154. /* This is the obstack to build an environment variable to pass to
  155.    collect2 that describes all of the relevant switches of what to
  156.    pass the compiler in building the list of pointers to constructors
  157.    and destructors.  */
  158.  
  159. static struct obstack collect_obstack;
  160.  
  161. extern char *version_string;
  162.  
  163. static void set_spec ();
  164. static struct compiler *lookup_compiler ();
  165. static char *find_a_file ();
  166. static void add_prefix ();
  167. static char *skip_whitespace ();
  168. static void record_temp_file ();
  169. static char *handle_braces ();
  170. static char *save_string ();
  171. static char *concat ();
  172. static int do_spec ();
  173. static int do_spec_1 ();
  174. static char *find_file ();
  175. static int is_linker_dir ();
  176. static void validate_switches ();
  177. static void validate_all_switches ();
  178. static void give_switch ();
  179. static void pfatal_with_name ();
  180. static void perror_with_name ();
  181. static void perror_exec ();
  182. static void fatal ();
  183. static void error ();
  184. void fancy_abort ();
  185. char *xmalloc ();
  186. char *xrealloc ();
  187.  
  188. /* Specs are strings containing lines, each of which (if not blank)
  189. is made up of a program name, and arguments separated by spaces.
  190. The program name must be exact and start from root, since no path
  191. is searched and it is unreliable to depend on the current working directory.
  192. Redirection of input or output is not supported; the subprograms must
  193. accept filenames saying what files to read and write.
  194.  
  195. In addition, the specs can contain %-sequences to substitute variable text
  196. or for conditional text.  Here is a table of all defined %-sequences.
  197. Note that spaces are not generated automatically around the results of
  198. expanding these sequences; therefore, you can concatenate them together
  199. or with constant text in a single argument.
  200.  
  201.  %%    substitute one % into the program name or argument.
  202.  %i     substitute the name of the input file being processed.
  203.  %b     substitute the basename of the input file being processed.
  204.     This is the substring up to (and not including) the last period
  205.     and not including the directory.
  206.  %g     substitute the temporary-file-name-base.  This is a string chosen
  207.     once per compilation.  Different temporary file names are made by
  208.     concatenation of constant strings on the end, as in `%g.s'.
  209.     %g also has the same effect of %d.
  210.  %u    like %g, but make the temporary file name unique.
  211.  %U    returns the last file name generated with %u.
  212.  %d    marks the argument containing or following the %d as a
  213.     temporary file name, so that that file will be deleted if CC exits
  214.     successfully.  Unlike %g, this contributes no text to the argument.
  215.  %w    marks the argument containing or following the %w as the
  216.     "output file" of this compilation.  This puts the argument
  217.     into the sequence of arguments that %o will substitute later.
  218.  %W{...}
  219.     like %{...} but mark last argument supplied within
  220.     as a file to be deleted on failure.
  221.  %o    substitutes the names of all the output files, with spaces
  222.     automatically placed around them.  You should write spaces
  223.     around the %o as well or the results are undefined.
  224.     %o is for use in the specs for running the linker.
  225.     Input files whose names have no recognized suffix are not compiled
  226.     at all, but they are included among the output files, so they will
  227.     be linked.
  228.  %p    substitutes the standard macro predefinitions for the
  229.     current target machine.  Use this when running cpp.
  230.  %P    like %p, but puts `__' before and after the name of each macro.
  231.     (Except macros that already have __.)
  232.     This is for ANSI C.
  233.  %I    Substitute a -iprefix option made from GCC_EXEC_PREFIX.
  234.  %s     current argument is the name of a library or startup file of some sort.
  235.         Search for that file in a standard list of directories
  236.     and substitute the full name found.
  237.  %eSTR  Print STR as an error message.  STR is terminated by a newline.
  238.         Use this when inconsistent options are detected.
  239.  %x{OPTION}    Accumulate an option for %X.
  240.  %X    Output the accumulated linker options specified by compilations.
  241.  %Y    Output the accumulated assembler options specified by compilations.
  242.  %a     process ASM_SPEC as a spec.
  243.         This allows config.h to specify part of the spec for running as.
  244.  %A    process ASM_FINAL_SPEC as a spec.  A capital A is actually
  245.     used here.  This can be used to run a post-processor after the
  246.     assembler has done it's job.
  247.  %D    Dump out a -L option for each directory in library_prefix,
  248.     followed by a -L option for each directory in startfile_prefix.
  249.  %l     process LINK_SPEC as a spec.
  250.  %L     process LIB_SPEC as a spec.
  251.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  252.  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
  253.  %c    process SIGNED_CHAR_SPEC as a spec.
  254.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  255.  %1    process CC1_SPEC as a spec.
  256.  %2    process CC1PLUS_SPEC as a spec.
  257.  %*    substitute the variable part of a matched option.  (See below.)
  258.     Note that each comma in the substituted string is replaced by
  259.     a single space.
  260.  %{S}   substitutes the -S switch, if that switch was given to CC.
  261.     If that switch was not specified, this substitutes nothing.
  262.     Here S is a metasyntactic variable.
  263.  %{S*}  substitutes all the switches specified to CC whose names start
  264.     with -S.  This is used for -o, -D, -I, etc; switches that take
  265.     arguments.  CC considers `-o foo' as being one switch whose
  266.     name starts with `o'.  %{o*} would substitute this text,
  267.     including the space; thus, two arguments would be generated.
  268.  %{S*:X} substitutes X if one or more switches whose names start with -S are
  269.     specified to CC.  Note that the tail part of the -S option
  270.     (i.e. the part matched by the `*') will be substituted for each
  271.     occurrence of %* within X.
  272.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  273.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  274.  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
  275.  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
  276.  %{.S:X} substitutes X, but only if processing a file with suffix S.
  277.  %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
  278.  %(Spec) processes a specification defined in a specs file as *Spec:
  279.  %[Spec] as above, but put __ around -D arguments
  280.  
  281. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  282. other nested % constructs or spaces, or even newlines.  They are
  283. processed as usual, as described above.
  284.  
  285. The character | is used to indicate that a command should be piped to
  286. the following command, but only if -pipe is specified.
  287.  
  288. Note that it is built into CC which switches take arguments and which
  289. do not.  You might think it would be useful to generalize this to
  290. allow each compiler's spec to say which switches take arguments.  But
  291. this cannot be done in a consistent fashion.  CC cannot even decide
  292. which input files have been specified without knowing which switches
  293. take arguments, and it must know which input files to compile in order
  294. to tell which compilers to run.
  295.  
  296. CC also knows implicitly that arguments starting in `-l' are to be
  297. treated as compiler output files, and passed to the linker in their
  298. proper position among the other output files.  */
  299.  
  300. /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1.  */
  301.  
  302. /* config.h can define ASM_SPEC to provide extra args to the assembler
  303.    or extra switch-translations.  */
  304. #ifndef ASM_SPEC
  305. #define ASM_SPEC ""
  306. #endif
  307.  
  308. /* config.h can define ASM_FINAL_SPEC to run a post processor after
  309.    the assembler has run.  */
  310. #ifndef ASM_FINAL_SPEC
  311. #define ASM_FINAL_SPEC ""
  312. #endif
  313.  
  314. /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
  315.    or extra switch-translations.  */
  316. #ifndef CPP_SPEC
  317. #define CPP_SPEC ""
  318. #endif
  319.  
  320. /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
  321.    or extra switch-translations.  */
  322. #ifndef CC1_SPEC
  323. #define CC1_SPEC ""
  324. #endif
  325.  
  326. /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
  327.    or extra switch-translations.  */
  328. #ifndef CC1PLUS_SPEC
  329. #define CC1PLUS_SPEC ""
  330. #endif
  331.  
  332. /* config.h can define LINK_SPEC to provide extra args to the linker
  333.    or extra switch-translations.  */
  334. #ifndef LINK_SPEC
  335. #define LINK_SPEC ""
  336. #endif
  337.  
  338. /* config.h can define LIB_SPEC to override the default libraries.  */
  339. #ifndef LIB_SPEC
  340. #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  341. #endif
  342.  
  343. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  344. #ifndef STARTFILE_SPEC
  345. #define STARTFILE_SPEC  \
  346.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  347. #endif
  348.  
  349. /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
  350.    Make the string nonempty to require spaces there.  */
  351. #ifndef SWITCHES_NEED_SPACES
  352. #define SWITCHES_NEED_SPACES ""
  353. #endif
  354.  
  355. /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
  356. #ifndef ENDFILE_SPEC
  357. #define ENDFILE_SPEC ""
  358. #endif
  359.  
  360. /* This spec is used for telling cpp whether char is signed or not.  */
  361. #ifndef SIGNED_CHAR_SPEC
  362. /* Use #if rather than ?:
  363.    because MIPS C compiler rejects like ?: in initializers.  */
  364. #if DEFAULT_SIGNED_CHAR
  365. #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
  366. #else
  367. #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
  368. #endif
  369. #endif
  370.  
  371. static char *cpp_spec = CPP_SPEC;
  372. static char *cpp_predefines = CPP_PREDEFINES;
  373. static char *cc1_spec = CC1_SPEC;
  374. static char *cc1plus_spec = CC1PLUS_SPEC;
  375. static char *signed_char_spec = SIGNED_CHAR_SPEC;
  376. static char *asm_spec = ASM_SPEC;
  377. static char *asm_final_spec = ASM_FINAL_SPEC;
  378. static char *link_spec = LINK_SPEC;
  379. static char *lib_spec = LIB_SPEC;
  380. static char *endfile_spec = ENDFILE_SPEC;
  381. static char *startfile_spec = STARTFILE_SPEC;
  382. static char *switches_need_spaces = SWITCHES_NEED_SPACES;
  383.  
  384. /* This defines which switch letters take arguments.  */
  385.  
  386. #ifndef SWITCH_TAKES_ARG
  387. #define SWITCH_TAKES_ARG(CHAR)      \
  388.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  389.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  390.    || (CHAR) == 'I' || (CHAR) == 'm' \
  391.    || (CHAR) == 'L' || (CHAR) == 'A')
  392. #endif
  393.  
  394. /* This defines which multi-letter switches take arguments.  */
  395.  
  396. #ifndef WORD_SWITCH_TAKES_ARG
  397. #define WORD_SWITCH_TAKES_ARG(STR)            \
  398.  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")    \
  399.   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")    \
  400.   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
  401. #endif
  402.  
  403. /* Record the mapping from file suffixes for compilation specs.  */
  404.  
  405. struct compiler
  406. {
  407.   char *suffix;            /* Use this compiler for input files
  408.                    whose names end in this suffix.  */
  409.  
  410.   char *spec[10];        /* To use this compiler, concatenate these
  411.                    specs and pass to do_spec.  */
  412. };
  413.  
  414. /* Pointer to a vector of `struct compiler' that gives the spec for
  415.    compiling a file, based on its suffix.
  416.    A file that does not end in any of these suffixes will be passed
  417.    unchanged to the loader and nothing else will be done to it.
  418.  
  419.    An entry containing two 0s is used to terminate the vector.
  420.  
  421.    If multiple entries match a file, the last matching one is used.  */
  422.  
  423. static struct compiler *compilers;
  424.  
  425. /* Number of entries in `compilers', not counting the null terminator.  */
  426.  
  427. static int n_compilers;
  428.  
  429. /* The default list of file name suffixes and their compilation specs.  */
  430.  
  431. static struct compiler default_compilers[] =
  432. {
  433.   {".c", "@c"},
  434.   {"@c",
  435.    "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  436.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  437.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  438.         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}",
  439.        "%{!undef:%{!ansi:%p} %P} %{trigraphs} \
  440.         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  441.         %{traditional-cpp:-traditional}\
  442.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  443.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  444.    "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
  445.            %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
  446.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  447.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
  448.            %{aux-info*}\
  449.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  450.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  451.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  452.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  453.                       %{!pipe:%g.s} %A\n }}}}"},
  454.   {"-",
  455.    "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  456.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  457.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  458.         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  459.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  460.         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  461.         %{traditional-cpp:-traditional}\
  462.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  463.         %i %W{o*}}\
  464.     %{!E:%e-E required when input is from standard input}"},
  465.   {".m", "@objective-c"},
  466.   {"@objective-c",
  467.    "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  468.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  469.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
  470.         -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}",
  471.        "%{!undef:%{!ansi:%p} %P} %{trigraphs}\
  472.         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  473.         %{traditional-cpp:-traditional}\
  474.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  475.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  476.    "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
  477.            %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
  478.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
  479.            %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} ",
  480.               "-lang-objc %{gen-decls} \
  481.            %{aux-info*}\
  482.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  483.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  484.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  485.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  486.                       %{!pipe:%g.s} %A\n }}}}"},
  487.   {".h", "@c-header"},
  488.   {"@c-header",
  489.    "%{!E:%eCompilation of header file requested} \
  490.     cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  491.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  492.      %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
  493.         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
  494.     %{!undef:%{!ansi:%p} %P} %{trigraphs}\
  495.         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  496.         %{traditional-cpp:-traditional}\
  497.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  498.         %i %W{o*}"},
  499. #ifdef MPW
  500.   {".cp", "@c++"},
  501. #endif
  502.   {".cc", "@c++"},
  503.   {".cxx", "@c++"},
  504.   {".C", "@c++"},
  505.   {"@c++",
  506.    "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  507.     %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
  508.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
  509.     -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
  510.     %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}",
  511.        "%c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  512.         %{traditional-cpp:-traditional} %{trigraphs}\
  513.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  514.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  515.    "%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
  516.            %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
  517.            %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  518.            %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
  519.            %{aux-info*}\
  520.            %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  521.            %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  522.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  523.               %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  524.                       %{!pipe:%g.s} %A\n }}}}"},
  525.   {".i", "@cpp-output"},
  526.   {"@cpp-output",
  527.    "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
  528.     %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  529.     %{v:-version} %{pg:-p} %{p} %{f*}\
  530.     %{aux-info*}\
  531.     %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  532.     %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  533.     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  534.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %{!pipe:%g.s} %A\n }"},
  535.   {".ii", "@c++-cpp-output"},
  536.   {"@c++-cpp-output",
  537.    "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
  538.         %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
  539.         %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
  540.         %{aux-info*}\
  541.         %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
  542.         %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
  543.        %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  544.            %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  545.            %{!pipe:%g.s} %A\n }"},
  546.   {".s", "@assembler"},
  547.   {"@assembler",
  548.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  549.             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %i %A\n }"},
  550.   {".S", "@assembler-with-cpp"},
  551.   {"@assembler-with-cpp",
  552.    "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
  553.     %{C:%{!E:%eGNU C does not support -C without using -E}}\
  554.     %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
  555.         -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
  556.         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
  557.         %{traditional-cpp:-traditional}\
  558.     %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
  559.         %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
  560.    "%{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
  561.                     %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
  562.             %{!pipe:%g.s} %A\n }}}}"},
  563.   /* Mark end of table */
  564.   {0, 0}
  565. };
  566.  
  567. /* Number of elements in default_compilers, not counting the terminator.  */
  568.  
  569. static int n_default_compilers
  570.   = (sizeof default_compilers / sizeof (struct compiler)) - 1;
  571.  
  572. /* Here is the spec for running the linker, after compiling all files.  */
  573.  
  574. /* -u* was put back because both BSD and SysV seem to support it.  */
  575. /* %{static:} simply prevents an error message if the target machine
  576.    doesn't handle -static.  */
  577. #ifdef LINK_LIBGCC_SPECIAL_1
  578. /* Have gcc do the search for libgcc.a, but generate -L options as usual.  */
  579. static char *link_command_spec = "\
  580. %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  581.             %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
  582.             %{!A:%{!nostdlib:%S}} %{static:}\
  583.             %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
  584. #else
  585. #ifdef LINK_LIBGCC_SPECIAL
  586. /* Have gcc do the search for libgcc.a, and don't generate -L options.  */
  587. static char *link_command_spec = "\
  588. %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  589.             %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
  590.             %{!A:%{!nostdlib:%S}} %{static:}\
  591.             %{L*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
  592. #else
  593. /* Use -L and have the linker do the search for -lgcc.  */
  594. static char *link_command_spec = "\
  595. %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
  596.             %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
  597.             %{!A:%{!nostdlib:%S}} %{static:}\
  598.             %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
  599. #endif
  600. #endif
  601.  
  602. /* A vector of options to give to the linker.
  603.    These options are accumulated by -Xlinker and -Wl,
  604.    and substituted into the linker command with %X.  */
  605. static int n_linker_options;
  606. static char **linker_options;
  607.  
  608. /* A vector of options to give to the assembler.
  609.    These options are accumulated by -Wa,
  610.    and substituted into the assembler command with %X.  */
  611. static int n_assembler_options;
  612. static char **assembler_options;
  613.  
  614. /* Read compilation specs from a file named FILENAME,
  615.    replacing the default ones.
  616.  
  617.    A suffix which starts with `*' is a definition for
  618.    one of the machine-specific sub-specs.  The "suffix" should be
  619.    *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
  620.    The corresponding spec is stored in asm_spec, etc.,
  621.    rather than in the `compilers' vector.
  622.  
  623.    Anything invalid in the file is a fatal error.  */
  624.  
  625. static void
  626. read_specs (filename)
  627.      char *filename;
  628. {
  629. #if 0
  630.   int desc;
  631.   struct stat statbuf;
  632.   char *buffer;
  633.   register char *p;
  634.  
  635.   if (verbose_flag)
  636.     fprintf (stderr, "Reading specs from %s\n", filename);
  637.  
  638.   /* Open and stat the file.  */
  639.   desc = open (filename, 0, 0);
  640.   if (desc < 0)
  641.     pfatal_with_name (filename);
  642.   if (stat (filename, &statbuf) < 0)
  643.     pfatal_with_name (filename);
  644.  
  645.   /* Read contents of file into BUFFER.  */
  646.   buffer = xmalloc ((unsigned) statbuf.st_size + 1);
  647.   read (desc, buffer, (unsigned) statbuf.st_size);
  648.   buffer[statbuf.st_size] = 0;
  649.   close (desc);
  650.  
  651.   /* Scan BUFFER for specs, putting them in the vector.  */
  652.   p = buffer;
  653.   while (1)
  654.     {
  655.       char *suffix;
  656.       char *spec;
  657.       char *in, *out, *p1, *p2;
  658.  
  659.       /* Advance P in BUFFER to the next nonblank nocomment line.  */
  660.       p = skip_whitespace (p);
  661.       if (*p == 0)
  662.     break;
  663.  
  664.       /* Find the colon that should end the suffix.  */
  665.       p1 = p;
  666.       while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
  667.       /* The colon shouldn't be missing.  */
  668.       if (*p1 != ':')
  669.     fatal ("specs file malformed after %d characters", p1 - buffer);
  670.       /* Skip back over trailing whitespace.  */
  671.       p2 = p1;
  672.       while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
  673.       /* Copy the suffix to a string.  */
  674.       suffix = save_string (p, p2 - p);
  675.       /* Find the next line.  */
  676.       p = skip_whitespace (p1 + 1);
  677.       if (p[1] == 0)
  678.     fatal ("specs file malformed after %d characters", p - buffer);
  679.       p1 = p;
  680.       /* Find next blank line.  */
  681.       while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
  682.       /* Specs end at the blank line and do not include the newline.  */
  683.       spec = save_string (p, p1 - p);
  684.       p = p1;
  685.  
  686.       /* Delete backslash-newline sequences from the spec.  */
  687.       in = spec;
  688.       out = spec;
  689.       while (*in != 0)
  690.     {
  691.       if (in[0] == '\\' && in[1] == '\n')
  692.         in += 2;
  693.       else if (in[0] == '#')
  694.         {
  695.           while (*in && *in != '\n') in++;
  696.         }
  697.       else
  698.         *out++ = *in++;
  699.     }
  700.       *out = 0;
  701.  
  702.       if (suffix[0] == '*')
  703.     {
  704.       if (! strcmp (suffix, "*link_command"))
  705.         link_command_spec = spec;
  706.       else
  707.         set_spec (suffix + 1, spec);
  708.     }
  709.       else
  710.     {
  711.       /* Add this pair to the vector.  */
  712.       compilers
  713.         = ((struct compiler *)
  714.            xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
  715.       compilers[n_compilers].suffix = suffix;
  716.       bzero (compilers[n_compilers].spec,
  717.          sizeof compilers[n_compilers].spec);
  718.       compilers[n_compilers].spec[0] = spec;
  719.       n_compilers++;
  720.     }
  721.  
  722.       if (*suffix == 0)
  723.     link_command_spec = spec;
  724.     }
  725.  
  726.   if (link_command_spec == 0)
  727.     fatal ("spec file has no spec for linking");
  728. #endif
  729. }
  730.  
  731. static char *
  732. skip_whitespace (p)
  733.      char *p;
  734. {
  735.   while (1)
  736.     {
  737.       /* A fully-blank line is a delimiter in the SPEC file and shouldn't
  738.      be considered whitespace.  */
  739.       if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
  740.     return p + 1;
  741.       else if (*p == '\n' || *p == ' ' || *p == '\t')
  742.     p++;
  743.       else if (*p == '#')
  744.     {
  745.       while (*p != '\n') p++;
  746.       p++;
  747.     }
  748.       else
  749.     break;
  750.     }
  751.  
  752.   return p;
  753. }
  754.  
  755. /* Structure to keep track of the specs that have been defined so far.  These
  756.    are accessed using %(specname) or %[specname] in a compiler or link spec. */
  757.  
  758. struct spec_list
  759. {
  760.   char *name;                 /* Name of the spec. */
  761.   char *spec;                 /* The spec itself. */
  762.   struct spec_list *next;     /* Next spec in linked list. */
  763. };
  764.  
  765. /* List of specs that have been defined so far. */
  766.  
  767. static struct spec_list *specs = (struct spec_list *) 0;
  768.  
  769. /* Change the value of spec NAME to SPEC.  If SPEC is empty, then the spec is
  770.    removed; If the spec starts with a + then SPEC is added to the end of the
  771.    current spec. */
  772.  
  773. static void
  774. set_spec (name, spec)
  775.      char *name;
  776.      char *spec;
  777. {
  778.   struct spec_list *sl;
  779.   char *old_spec;
  780.  
  781.   /* See if the spec already exists */
  782.   for (sl = specs; sl; sl = sl->next)
  783.     if (strcmp (sl->name, name) == 0)
  784.       break;
  785.  
  786.   if (!sl)
  787.     {
  788.       /* Not found - make it */
  789.       sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
  790.       sl->name = save_string (name, strlen (name));
  791.       sl->spec = save_string ("", 0);
  792.       sl->next = specs;
  793.       specs = sl;
  794.     }
  795.  
  796.   old_spec = sl->spec;
  797.   if (name && spec[0] == '+' && isspace (spec[1]))
  798.     sl->spec = concat (old_spec, spec + 1, "");
  799.   else
  800.     sl->spec = save_string (spec, strlen (spec));
  801.  
  802.   if (! strcmp (name, "asm"))
  803.     asm_spec = sl->spec;
  804.   else if (! strcmp (name, "asm_final"))
  805.     asm_final_spec = sl->spec;
  806.   else if (! strcmp (name, "cc1"))
  807.     cc1_spec = sl->spec;
  808.   else if (! strcmp (name, "cc1plus"))
  809.     cc1plus_spec = sl->spec;
  810.   else if (! strcmp (name, "cpp"))
  811.     cpp_spec = sl->spec;
  812.   else if (! strcmp (name, "endfile"))
  813.     endfile_spec = sl->spec;
  814.   else if (! strcmp (name, "lib"))
  815.     lib_spec = sl->spec;
  816.   else if (! strcmp (name, "link"))
  817.     link_spec = sl->spec;
  818.   else if (! strcmp (name, "predefines"))
  819.     cpp_predefines = sl->spec;
  820.   else if (! strcmp (name, "signed_char"))
  821.     signed_char_spec = sl->spec;
  822.   else if (! strcmp (name, "startfile"))
  823.     startfile_spec = sl->spec;
  824.   else if (! strcmp (name, "switches_need_spaces"))
  825.     switches_need_spaces = sl->spec;
  826.   else if (! strcmp (name, "cross_compile"))
  827.     cross_compile = atoi (sl->spec);
  828.   /* Free the old spec */
  829.   if (old_spec)
  830.     free (old_spec);
  831. }
  832.  
  833. /* Accumulate a command (program name and args), and run it.  */
  834.  
  835. /* Vector of pointers to arguments in the current line of specifications.  */
  836.  
  837. static char **argbuf;
  838.  
  839. /* Number of elements allocated in argbuf.  */
  840.  
  841. static int argbuf_length;
  842.  
  843. /* Number of elements in argbuf currently in use (containing args).  */
  844.  
  845. static int argbuf_index;
  846.  
  847. /* This is the list of suffixes and codes (%g/%u/%U) and the associated
  848.    temp file.  Used only if MKTEMP_EACH_FILE.  */
  849.  
  850. static struct temp_name {
  851.   char *suffix;        /* suffix associated with the code.  */
  852.   int length;        /* strlen (suffix).  */
  853.   int unique;        /* Indicates whether %g or %u/%U was used.  */
  854.   char *filename;    /* associated filename.  */
  855.   int filename_length;    /* strlen (filename).  */
  856.   struct temp_name *next;
  857. } *temp_names;
  858.  
  859. /* Number of commands executed so far.  */
  860.  
  861. static int execution_count;
  862.  
  863. /* Number of commands that exited with a signal.  */
  864.  
  865. static int signal_count;
  866.  
  867. /* Name with which this program was invoked.  */
  868.  
  869. static char *programname;
  870.  
  871. /* Structures to keep track of prefixes to try when looking for files. */
  872.  
  873. struct prefix_list
  874. {
  875.   char *prefix;               /* String to prepend to the path. */
  876.   struct prefix_list *next;   /* Next in linked list. */
  877.   int require_machine_suffix; /* Don't use without machine_suffix.  */
  878.   /* 2 means try both machine_suffix and just_machine_suffix.  */
  879.   int *used_flag_ptr;          /* 1 if a file was found with this prefix.  */
  880. };
  881.  
  882. struct path_prefix
  883. {
  884.   struct prefix_list *plist;  /* List of prefixes to try */
  885.   int max_len;                /* Max length of a prefix in PLIST */
  886.   char *name;                 /* Name of this list (used in config stuff) */
  887. };
  888.  
  889. /* List of prefixes to try when looking for executables. */
  890.  
  891. static struct path_prefix exec_prefix = { 0, 0, "exec" };
  892.  
  893. /* List of prefixes to try when looking for startup (crt0) files. */
  894.  
  895. static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
  896.  
  897. /* List of prefixes to try when looking for libraries. */
  898.  
  899. static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
  900.  
  901. /* Suffix to attach to directories searched for commands.
  902.    This looks like `MACHINE/VERSION/'.  */
  903.  
  904. static char *machine_suffix = 0;
  905.  
  906. /* Suffix to attach to directories searched for commands.
  907.    This is just `MACHINE/'.  */
  908.  
  909. static char *just_machine_suffix = 0;
  910.  
  911. /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
  912.  
  913. static char *gcc_exec_prefix;
  914.  
  915. /* Default prefixes to attach to command names.  */
  916.  
  917. #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
  918. #undef MD_EXEC_PREFIX
  919. #undef MD_STARTFILE_PREFIX
  920. #undef MD_STARTFILE_PREFIX_1
  921. #endif
  922.  
  923. #ifndef STANDARD_EXEC_PREFIX
  924. #define STANDARD_EXEC_PREFIX "{MPW}GCC-Lib"
  925. #endif /* !defined STANDARD_EXEC_PREFIX */
  926.  
  927. static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  928. static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
  929. #ifdef MD_EXEC_PREFIX
  930. static char *md_exec_prefix = MD_EXEC_PREFIX;
  931. #endif
  932.  
  933. #ifndef STANDARD_STARTFILE_PREFIX
  934. #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
  935. #endif /* !defined STANDARD_STARTFILE_PREFIX */
  936.  
  937. #ifdef MD_STARTFILE_PREFIX
  938. static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
  939. #endif
  940. #ifdef MD_STARTFILE_PREFIX_1
  941. static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
  942. #endif
  943. static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
  944. static char *standard_startfile_prefix_1 = "/lib/";
  945. static char *standard_startfile_prefix_2 = "/usr/lib/";
  946.  
  947. /* Clear out the vector of arguments (after a command is executed).  */
  948.  
  949. static void
  950. clear_args ()
  951. {
  952.   argbuf_index = 0;
  953. }
  954.  
  955. /* Add one argument to the vector at the end.
  956.    This is done when a space is seen or at the end of the line.
  957.    If DELETE_ALWAYS is nonzero, the arg is a filename
  958.     and the file should be deleted eventually.
  959.    If DELETE_FAILURE is nonzero, the arg is a filename
  960.     and the file should be deleted if this compilation fails.  */
  961.  
  962. static void
  963. store_arg (arg, delete_always, delete_failure)
  964.      char *arg;
  965.      int delete_always, delete_failure;
  966. {
  967.   if (argbuf_index + 1 == argbuf_length)
  968.     {
  969.       argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  970.     }
  971.  
  972.   argbuf[argbuf_index++] = arg;
  973.   argbuf[argbuf_index] = 0;
  974.  
  975.   if (delete_always || delete_failure)
  976.     record_temp_file (arg, delete_always, delete_failure);
  977. }
  978.  
  979. /* Record the names of temporary files we tell compilers to write,
  980.    and delete them at the end of the run.  */
  981.  
  982. /* This is the common prefix we use to make temp file names.
  983.    It is chosen once for each run of this program.
  984.    It is substituted into a spec by %g.
  985.    Thus, all temp file names contain this prefix.
  986.    In practice, all temp file names start with this prefix.
  987.  
  988.    This prefix comes from the envvar TMPDIR if it is defined;
  989.    otherwise, from the P_tmpdir macro if that is defined;
  990.    otherwise, in /usr/tmp or /tmp.  */
  991.  
  992. static char *temp_filename;
  993.  
  994. /* Length of the prefix.  */
  995.  
  996. static int temp_filename_length;
  997.  
  998. /* Define the list of temporary files to delete.  */
  999.  
  1000. struct temp_file
  1001. {
  1002.   char *name;
  1003.   struct temp_file *next;
  1004. };
  1005.  
  1006. /* Queue of files to delete on success or failure of compilation.  */
  1007. static struct temp_file *always_delete_queue;
  1008. /* Queue of files to delete on failure of compilation.  */
  1009. static struct temp_file *failure_delete_queue;
  1010.  
  1011. /* Record FILENAME as a file to be deleted automatically.
  1012.    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
  1013.    otherwise delete it in any case.
  1014.    FAIL_DELETE nonzero means delete it if a compilation step fails;
  1015.    otherwise delete it in any case.  */
  1016.  
  1017. static void
  1018. record_temp_file (filename, always_delete, fail_delete)
  1019.      char *filename;
  1020.      int always_delete;
  1021.      int fail_delete;
  1022. {
  1023.   register char *name;
  1024.   name = xmalloc (strlen (filename) + 1);
  1025.   strcpy (name, filename);
  1026.  
  1027.   if (always_delete)
  1028.     {
  1029.       register struct temp_file *temp;
  1030.       for (temp = always_delete_queue; temp; temp = temp->next)
  1031.     if (! strcmp (name, temp->name))
  1032.       goto already1;
  1033.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1034.       temp->next = always_delete_queue;
  1035.       temp->name = name;
  1036.       always_delete_queue = temp;
  1037.     already1:;
  1038.     }
  1039.  
  1040.   if (fail_delete)
  1041.     {
  1042.       register struct temp_file *temp;
  1043.       for (temp = failure_delete_queue; temp; temp = temp->next)
  1044.     if (! strcmp (name, temp->name))
  1045.       goto already2;
  1046.       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  1047.       temp->next = failure_delete_queue;
  1048.       temp->name = name;
  1049.       failure_delete_queue = temp;
  1050.     already2:;
  1051.     }
  1052. }
  1053.  
  1054. /* Delete all the temporary files whose names we previously recorded.  */
  1055.  
  1056. static void
  1057. delete_temp_files ()
  1058. {
  1059.   register struct temp_file *temp;
  1060.  
  1061.   for (temp = always_delete_queue; temp; temp = temp->next)
  1062.     {
  1063. #ifdef DEBUG
  1064.       int i;
  1065.       printf ("Delete %s? (y or n) ", temp->name);
  1066.       fflush (stdout);
  1067.       i = getchar ();
  1068.       if (i != '\n')
  1069.     while (getchar () != '\n') ;
  1070.       if (i == 'y' || i == 'Y')
  1071. #endif /* DEBUG */
  1072. #ifndef MPW
  1073.     {
  1074.       struct stat st;
  1075.       if (stat (temp->name, &st) >= 0)
  1076.         {
  1077.           /* Delete only ordinary files.  */
  1078.           if (S_ISREG (st.st_mode))
  1079.         if (unlink (temp->name) < 0)
  1080.           if (verbose_flag)
  1081.             perror_with_name (temp->name);
  1082.         }
  1083.     }
  1084. #endif
  1085.     }
  1086.  
  1087.   always_delete_queue = 0;
  1088. }
  1089.  
  1090. /* Delete all the files to be deleted on error.  */
  1091.  
  1092. static void
  1093. delete_failure_queue ()
  1094. {
  1095.   register struct temp_file *temp;
  1096.  
  1097.   for (temp = failure_delete_queue; temp; temp = temp->next)
  1098.     {
  1099. #ifdef DEBUG
  1100.       int i;
  1101.       printf ("Delete %s? (y or n) ", temp->name);
  1102.       fflush (stdout);
  1103.       i = getchar ();
  1104.       if (i != '\n')
  1105.     while (getchar () != '\n') ;
  1106.       if (i == 'y' || i == 'Y')
  1107. #endif /* DEBUG */
  1108.     {
  1109.       if (unlink (temp->name) < 0)
  1110.         if (verbose_flag)
  1111.           perror_with_name (temp->name);
  1112.     }
  1113.     }
  1114. }
  1115.  
  1116. static void
  1117. clear_failure_queue ()
  1118. {
  1119.   failure_delete_queue = 0;
  1120. }
  1121.  
  1122. /* Compute a string to use as the base of all temporary file names.
  1123.    It is substituted for %g.  */
  1124.  
  1125. static void
  1126. choose_temp_base ()
  1127. {
  1128.   char *base = getenv ("TMPDIR");
  1129.   int len;
  1130.  
  1131.   if (base == (char *)0)
  1132.     {
  1133. #ifdef P_tmpdir
  1134.       if (access (P_tmpdir, R_OK | W_OK) == 0)
  1135.     base = P_tmpdir;
  1136. #endif
  1137.       if (base == (char *)0)
  1138.     {
  1139.       if (access ("/usr/tmp", R_OK | W_OK) == 0)
  1140.         base = "/usr/tmp/";
  1141.       else
  1142.         base = "/tmp/";
  1143.     }
  1144.     }
  1145.  
  1146.   len = strlen (base);
  1147.   temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
  1148.   strcpy (temp_filename, base);
  1149.   if (len > 0 && temp_filename[len-1] != '/')
  1150.     temp_filename[len++] = '/';
  1151.   strcpy (temp_filename + len, "ccXXXXXX");
  1152.  
  1153.   mktemp (temp_filename);
  1154.   temp_filename_length = strlen (temp_filename);
  1155.   if (temp_filename_length == 0)
  1156.     abort ();
  1157. }
  1158.  
  1159.  
  1160. /* Routine to add variables to the environment.  We do this to pass
  1161.    the pathname of the gcc driver, and the directories search to the
  1162.    collect2 program, which is being run as ld.  This way, we can be
  1163.    sure of executing the right compiler when collect2 wants to build
  1164.    constructors and destructors.  Since the environment variables we
  1165.    use come from an obstack, we don't have to worry about allocating
  1166.    space for them.  */
  1167.  
  1168. #ifndef HAVE_PUTENV
  1169.  
  1170. putenv (str)
  1171.      char *str;
  1172. {
  1173. #ifndef MPW
  1174. #ifndef VMS            /* nor about VMS */
  1175.  
  1176.   extern char **environ;
  1177.   char **old_environ = environ;
  1178.   char **envp;
  1179.   int num_envs = 0;
  1180.   int name_len = 1;
  1181.   int str_len = strlen (str);
  1182.   char *p = str;
  1183.   int ch;
  1184.  
  1185.   while ((ch = *p++) != '\0' && ch != '=')
  1186.     name_len++;
  1187.  
  1188.   if (!ch)
  1189.     abort ();
  1190.  
  1191.   /* Search for replacing an existing environment variable, and
  1192.      count the number of total environment variables.  */
  1193.   for (envp = old_environ; *envp; envp++)
  1194.     {
  1195.       num_envs++;
  1196.       if (!strncmp (str, *envp, name_len))
  1197.     {
  1198.       *envp = str;
  1199.       return;
  1200.     }
  1201.     }
  1202.  
  1203.   /* Add a new environment variable */
  1204.   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
  1205.   *environ = str;
  1206.   bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
  1207.  
  1208. #endif    /* VMS */
  1209. #endif
  1210. }
  1211.  
  1212. #endif    /* HAVE_PUTENV */
  1213.  
  1214.  
  1215. /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect.  */
  1216.  
  1217. static void
  1218. putenv_from_prefixes (paths, env_var)
  1219.      struct path_prefix *paths;
  1220.      char *env_var;
  1221. {
  1222.   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
  1223.   int first_time = TRUE;
  1224.   struct prefix_list *pprefix;
  1225.  
  1226.   obstack_grow (&collect_obstack, env_var, strlen (env_var));
  1227.  
  1228.   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
  1229.     {
  1230.       int len = strlen (pprefix->prefix);
  1231.  
  1232.       if (machine_suffix)
  1233.     {
  1234.       if (!first_time)
  1235.         obstack_grow (&collect_obstack, ":", 1);
  1236.         
  1237.       first_time = FALSE;
  1238.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1239.       obstack_grow (&collect_obstack, machine_suffix, suffix_len);
  1240.     }
  1241.  
  1242.       if (just_machine_suffix && pprefix->require_machine_suffix == 2)
  1243.     {
  1244.       if (!first_time)
  1245.         obstack_grow (&collect_obstack, ":", 1);
  1246.         
  1247.       first_time = FALSE;
  1248.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1249.       obstack_grow (&collect_obstack, machine_suffix, suffix_len);
  1250.     }
  1251.  
  1252.       if (!pprefix->require_machine_suffix)
  1253.     {
  1254.       if (!first_time)
  1255.         obstack_grow (&collect_obstack, ":", 1);
  1256.  
  1257.       first_time = FALSE;
  1258.       obstack_grow (&collect_obstack, pprefix->prefix, len);
  1259.     }
  1260.     }
  1261.   obstack_grow (&collect_obstack, "\0", 1);
  1262.   putenv (obstack_finish (&collect_obstack));
  1263. }
  1264.  
  1265.  
  1266. /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
  1267.    access to check permissions.
  1268.    Return 0 if not found, otherwise return its name, allocated with malloc. */
  1269.  
  1270. static char *
  1271. find_a_file (pprefix, name, mode)
  1272.      struct path_prefix *pprefix;
  1273.      char *name;
  1274.      int mode;
  1275. {
  1276.   char *temp;
  1277.   char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
  1278.   struct prefix_list *pl;
  1279.   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
  1280.  
  1281.   if (machine_suffix)
  1282.     len += strlen (machine_suffix);
  1283.  
  1284.   temp = xmalloc (len);
  1285.  
  1286.   /* Determine the filename to execute (special case for absolute paths).  */
  1287.  
  1288.   if (*name == '/')
  1289.     {
  1290.       if (access (name, mode))
  1291.     {
  1292.       strcpy (temp, name);
  1293.       return temp;
  1294.     }
  1295.     }
  1296.   else
  1297.     for (pl = pprefix->plist; pl; pl = pl->next)
  1298.       {
  1299.     if (machine_suffix)
  1300.       {
  1301.         strcpy (temp, pl->prefix);
  1302.         strcat (temp, machine_suffix);
  1303.         strcat (temp, name);
  1304.         if (access (temp, mode) == 0)
  1305.           {
  1306.         if (pl->used_flag_ptr != 0)
  1307.           *pl->used_flag_ptr = 1;
  1308.         return temp;
  1309.           }
  1310.         /* Some systems have a suffix for executable files.
  1311.            So try appending that.  */
  1312.         if (file_suffix[0] != 0)
  1313.           {
  1314.         strcat (temp, file_suffix);
  1315.         if (access (temp, mode) == 0)
  1316.           {
  1317.             if (pl->used_flag_ptr != 0)
  1318.               *pl->used_flag_ptr = 1;
  1319.             return temp;
  1320.           }
  1321.           }
  1322.       }
  1323.     /* Certain prefixes are tried with just the machine type,
  1324.        not the version.  This is used for finding as, ld, etc.  */
  1325.     if (just_machine_suffix && pl->require_machine_suffix == 2)
  1326.       {
  1327.         strcpy (temp, pl->prefix);
  1328.         strcat (temp, just_machine_suffix);
  1329.         strcat (temp, name);
  1330.         if (access (temp, mode) == 0)
  1331.           {
  1332.         if (pl->used_flag_ptr != 0)
  1333.           *pl->used_flag_ptr = 1;
  1334.         return temp;
  1335.           }
  1336.         /* Some systems have a suffix for executable files.
  1337.            So try appending that.  */
  1338.         if (file_suffix[0] != 0)
  1339.           {
  1340.         strcat (temp, file_suffix);
  1341.         if (access (temp, mode) == 0)
  1342.           {
  1343.             if (pl->used_flag_ptr != 0)
  1344.               *pl->used_flag_ptr = 1;
  1345.             return temp;
  1346.           }
  1347.           }
  1348.       }
  1349.     /* Certain prefixes can't be used without the machine suffix
  1350.        when the machine or version is explicitly specified.  */
  1351.     if (!pl->require_machine_suffix)
  1352.       {
  1353.         strcpy (temp, pl->prefix);
  1354.         strcat (temp, name);
  1355.         if (access (temp, mode) == 0)
  1356.           {
  1357.         if (pl->used_flag_ptr != 0)
  1358.           *pl->used_flag_ptr = 1;
  1359.         return temp;
  1360.           }
  1361.         /* Some systems have a suffix for executable files.
  1362.            So try appending that.  */
  1363.         if (file_suffix[0] != 0)
  1364.           {
  1365.         strcat (temp, file_suffix);
  1366.         if (access (temp, mode) == 0)
  1367.           {
  1368.             if (pl->used_flag_ptr != 0)
  1369.               *pl->used_flag_ptr = 1;
  1370.             return temp;
  1371.           }
  1372.           }
  1373.       }
  1374.       }
  1375.  
  1376.   free (temp);
  1377.   return 0;
  1378. }
  1379.  
  1380. /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
  1381.    at the start of the list, otherwise it goes at the end.
  1382.  
  1383.    If WARN is nonzero, we will warn if no file is found
  1384.    through this prefix.  WARN should point to an int
  1385.    which will be set to 1 if this entry is used.
  1386.  
  1387.    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
  1388.    the complete value of machine_suffix.
  1389.    2 means try both machine_suffix and just_machine_suffix.  */
  1390.  
  1391. static void
  1392. add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
  1393.      struct path_prefix *pprefix;
  1394.      char *prefix;
  1395.      int first;
  1396.      int require_machine_suffix;
  1397.      int *warn;
  1398. {
  1399.   struct prefix_list *pl, **prev;
  1400.   int len;
  1401.  
  1402.   if (!first && pprefix->plist)
  1403.     {
  1404.       for (pl = pprefix->plist; pl->next; pl = pl->next)
  1405.     ;
  1406.       prev = &pl->next;
  1407.     }
  1408.   else
  1409.     prev = &pprefix->plist;
  1410.  
  1411.   /* Keep track of the longest prefix */
  1412.  
  1413.   len = strlen (prefix);
  1414.   if (len > pprefix->max_len)
  1415.     pprefix->max_len = len;
  1416.  
  1417.   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
  1418.   pl->prefix = save_string (prefix, len);
  1419.   pl->require_machine_suffix = require_machine_suffix;
  1420.   pl->used_flag_ptr = warn;
  1421.   if (warn)
  1422.     *warn = 0;
  1423.  
  1424.   if (*prev)
  1425.     pl->next = *prev;
  1426.   else
  1427.     pl->next = (struct prefix_list *) 0;
  1428.   *prev = pl;
  1429. }
  1430.  
  1431. /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
  1432.  
  1433. static void
  1434. unused_prefix_warnings (pprefix)
  1435.      struct path_prefix *pprefix;
  1436. {
  1437.   struct prefix_list *pl = pprefix->plist;
  1438.  
  1439.   while (pl)
  1440.     {
  1441.       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
  1442.     {
  1443.       error ("file path prefix `%s' never used",
  1444.          pl->prefix);
  1445.       /* Prevent duplicate warnings.  */
  1446.       *pl->used_flag_ptr = 1;
  1447.     }
  1448.       pl = pl->next;
  1449.     }
  1450. }
  1451.  
  1452. /* Get rid of all prefixes built up so far in *PLISTP. */
  1453.  
  1454. static void
  1455. free_path_prefix (pprefix)
  1456.      struct path_prefix *pprefix;
  1457. {
  1458.   struct prefix_list *pl = pprefix->plist;
  1459.   struct prefix_list *temp;
  1460.  
  1461.   while (pl)
  1462.     {
  1463.       temp = pl;
  1464.       pl = pl->next;
  1465.       free (temp->prefix);
  1466.       free ((char *) temp);
  1467.     }
  1468.   pprefix->plist = (struct prefix_list *) 0;
  1469. }
  1470.  
  1471. /* stdin file number.  */
  1472. #define STDIN_FILE_NO 0
  1473.  
  1474. /* stdout file number.  */
  1475. #define STDOUT_FILE_NO 1
  1476.  
  1477. /* value of `pipe': port index for reading.  */
  1478. #define READ_PORT 0
  1479.  
  1480. /* value of `pipe': port index for writing.  */
  1481. #define WRITE_PORT 1
  1482.  
  1483. /* Pipe waiting from last process, to be used as input for the next one.
  1484.    Value is STDIN_FILE_NO if no pipe is waiting
  1485.    (i.e. the next command is the first of a group).  */
  1486.  
  1487. static int last_pipe_input;
  1488.  
  1489. /* Fork one piped subcommand.  FUNC is the system call to use
  1490.    (either execv or execvp).  ARGV is the arg vector to use.
  1491.    NOT_LAST is nonzero if this is not the last subcommand
  1492.    (i.e. its output should be piped to the next one.)  */
  1493.  
  1494. #ifdef MPW
  1495.  
  1496. int execv(const char *a, const char **b){}
  1497. int execvp(const char *a, const char **b){}
  1498.  
  1499. static int
  1500. pexecute (search_flag, program, argv, not_last)
  1501.      int search_flag;
  1502.      char *program;
  1503.      char *argv[];
  1504.      int not_last;
  1505. {
  1506.   printf("want to execute %s\n", program);
  1507.   return 1;
  1508. }
  1509. #else
  1510. #ifndef OS2
  1511. #ifdef __MSDOS__
  1512.  
  1513. /* Declare these to avoid compilation error.  They won't be called.  */
  1514. int execv(const char *a, const char **b){}
  1515. int execvp(const char *a, const char **b){}
  1516.  
  1517. static int
  1518. pexecute (search_flag, program, argv, not_last)
  1519.      int search_flag;
  1520.      char *program;
  1521.      char *argv[];
  1522.      int not_last;
  1523. {
  1524.   char *scmd;
  1525.   FILE *argfile;
  1526.   int i;
  1527.  
  1528.   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
  1529.   sprintf (scmd, "%s @%s.gp", program, temp_filename);
  1530.   argfile = fopen (scmd+strlen (program) + 2, "w");
  1531.   if (argfile == 0)
  1532.     pfatal_with_name (scmd + strlen (program) + 2);
  1533.  
  1534.   for (i=1; argv[i]; i++)
  1535.   {
  1536.     char *cp;
  1537.     for (cp = argv[i]; *cp; cp++)
  1538.       {
  1539.     if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
  1540.       fputc ('\\', argfile);
  1541.     fputc (*cp, argfile);
  1542.       }
  1543.     fputc ('\n', argfile);
  1544.   }
  1545.   fclose (argfile);
  1546.  
  1547.   i = system (scmd);
  1548.  
  1549.   remove (scmd + strlen (program) + 2);
  1550.   return i << 8;
  1551. }
  1552.  
  1553. #else /* not __MSDOS__ */
  1554.  
  1555. static int
  1556. pexecute (search_flag, program, argv, not_last)
  1557.      int search_flag;
  1558.      char *program;
  1559.      char *argv[];
  1560.      int not_last;
  1561. {
  1562.   int (*func)() = (search_flag ? execv : execvp);
  1563.   int pid;
  1564.   int pdes[2];
  1565.   int input_desc = last_pipe_input;
  1566.   int output_desc = STDOUT_FILE_NO;
  1567.   int retries, sleep_interval;
  1568.  
  1569.   /* If this isn't the last process, make a pipe for its output,
  1570.      and record it as waiting to be the input to the next process.  */
  1571.  
  1572.   if (not_last)
  1573.     {
  1574.       if (pipe (pdes) < 0)
  1575.     pfatal_with_name ("pipe");
  1576.       output_desc = pdes[WRITE_PORT];
  1577.       last_pipe_input = pdes[READ_PORT];
  1578.     }
  1579.   else
  1580.     last_pipe_input = STDIN_FILE_NO;
  1581.  
  1582.   /* Fork a subprocess; wait and retry if it fails.  */
  1583.   sleep_interval = 1;
  1584.   for (retries = 0; retries < 4; retries++)
  1585.     {
  1586.       pid = vfork ();
  1587.       if (pid >= 0)
  1588.     break;
  1589.       sleep (sleep_interval);
  1590.       sleep_interval *= 2;
  1591.     }
  1592.  
  1593.   switch (pid)
  1594.     {
  1595.     case -1:
  1596. #ifdef vfork
  1597.       pfatal_with_name ("fork");
  1598. #else
  1599.       pfatal_with_name ("vfork");
  1600. #endif
  1601.       /* NOTREACHED */
  1602.       return 0;
  1603.  
  1604.     case 0: /* child */
  1605.       /* Move the input and output pipes into place, if nec.  */
  1606.       if (input_desc != STDIN_FILE_NO)
  1607.     {
  1608.       close (STDIN_FILE_NO);
  1609.       dup (input_desc);
  1610.       close (input_desc);
  1611.     }
  1612.       if (output_desc != STDOUT_FILE_NO)
  1613.     {
  1614.       close (STDOUT_FILE_NO);
  1615.       dup (output_desc);
  1616.       close (output_desc);
  1617.     }
  1618.  
  1619.       /* Close the parent's descs that aren't wanted here.  */
  1620.       if (last_pipe_input != STDIN_FILE_NO)
  1621.     close (last_pipe_input);
  1622.  
  1623.       /* Exec the program.  */
  1624.       (*func) (program, argv);
  1625.       perror_exec (program);
  1626.       exit (-1);
  1627.       /* NOTREACHED */
  1628.       return 0;
  1629.  
  1630.     default:
  1631.       /* In the parent, after forking.
  1632.      Close the descriptors that we made for this child.  */
  1633.       if (input_desc != STDIN_FILE_NO)
  1634.     close (input_desc);
  1635.       if (output_desc != STDOUT_FILE_NO)
  1636.     close (output_desc);
  1637.  
  1638.       /* Return child's process number.  */
  1639.       return pid;
  1640.     }
  1641. }
  1642.  
  1643. #endif /* not __MSDOS__ */
  1644. #else /* not OS2 */
  1645.  
  1646. static int
  1647. pexecute (search_flag, program, argv, not_last)
  1648.      int search_flag;
  1649.      char *program;
  1650.      char *argv[];
  1651.      int not_last;
  1652. {
  1653.   return (search_flag ? spawnv : spawnvp) (1, program, argv);
  1654. }
  1655. #endif /* not OS2 */
  1656. #endif /* MPW */
  1657.  
  1658. /* Execute the command specified by the arguments on the current line of spec.
  1659.    When using pipes, this includes several piped-together commands
  1660.    with `|' between them.
  1661.  
  1662.    Return 0 if successful, -1 if failed.  */
  1663.  
  1664. static int
  1665. execute ()
  1666. {
  1667.   int i;
  1668.   int n_commands;        /* # of command.  */
  1669.   char *string;
  1670.   struct command
  1671.     {
  1672.       char *prog;        /* program name.  */
  1673.       char **argv;        /* vector of args.  */
  1674.       int pid;            /* pid of process for this command.  */
  1675.     };
  1676.  
  1677.   struct command *commands;    /* each command buffer with above info.  */
  1678.  
  1679.   /* Count # of piped commands.  */
  1680.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  1681.     if (strcmp (argbuf[i], "|") == 0)
  1682.       n_commands++;
  1683.  
  1684.   /* Get storage for each command.  */
  1685.   commands
  1686.     = (struct command *) alloca (n_commands * sizeof (struct command));
  1687.  
  1688.   /* Split argbuf into its separate piped processes,
  1689.      and record info about each one.
  1690.      Also search for the programs that are to be run.  */
  1691.  
  1692.   commands[0].prog = argbuf[0]; /* first command.  */
  1693.   commands[0].argv = &argbuf[0];
  1694.   string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
  1695.   if (string)
  1696.     commands[0].argv[0] = string;
  1697.  
  1698.   for (n_commands = 1, i = 0; i < argbuf_index; i++)
  1699.     if (strcmp (argbuf[i], "|") == 0)
  1700.       {                /* each command.  */
  1701. #ifdef __MSDOS__
  1702.         fatal ("-pipe not supported under MS-DOS");
  1703. #endif
  1704.     argbuf[i] = 0;    /* termination of command args.  */
  1705.     commands[n_commands].prog = argbuf[i + 1];
  1706.     commands[n_commands].argv = &argbuf[i + 1];
  1707.     string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
  1708.     if (string)
  1709.       commands[n_commands].argv[0] = string;
  1710.     n_commands++;
  1711.       }
  1712.  
  1713.   argbuf[argbuf_index] = 0;
  1714.  
  1715.   /* If -v, print what we are about to do, and maybe query.  */
  1716.  
  1717.   if (verbose_flag)
  1718.     {
  1719.       /* Print each piped command as a separate line.  */
  1720.       for (i = 0; i < n_commands ; i++)
  1721.     {
  1722.       char **j;
  1723.  
  1724.       for (j = commands[i].argv; *j; j++)
  1725.         fprintf (stderr, " %s", *j);
  1726.  
  1727.       /* Print a pipe symbol after all but the last command.  */
  1728.       if (i + 1 != n_commands)
  1729.         fprintf (stderr, " |");
  1730.       fprintf (stderr, "\n");
  1731.     }
  1732.       fflush (stderr);
  1733. #ifdef DEBUG
  1734.       fprintf (stderr, "\nGo ahead? (y or n) ");
  1735.       fflush (stderr);
  1736.       i = getchar ();
  1737.       if (i != '\n')
  1738.     while (getchar () != '\n') ;
  1739.       if (i != 'y' && i != 'Y')
  1740.     return 0;
  1741. #endif /* DEBUG */
  1742.     }
  1743.  
  1744.   /* Run each piped subprocess.  */
  1745.  
  1746.   last_pipe_input = STDIN_FILE_NO;
  1747.   for (i = 0; i < n_commands; i++)
  1748.     {
  1749.       char *string = commands[i].argv[0];
  1750.  
  1751.       commands[i].pid = pexecute (string != commands[i].prog,
  1752.                   string, commands[i].argv,
  1753.                   i + 1 < n_commands);
  1754.  
  1755.       if (string != commands[i].prog)
  1756.     free (string);
  1757.     }
  1758.  
  1759.   execution_count++;
  1760.  
  1761.   /* Wait for all the subprocesses to finish.
  1762.      We don't care what order they finish in;
  1763.      we know that N_COMMANDS waits will get them all.  */
  1764.  
  1765.   {
  1766.     int ret_code = 0;
  1767.  
  1768.     for (i = 0; i < n_commands; i++)
  1769.       {
  1770.     int status;
  1771.     int pid;
  1772.     char *prog;
  1773.  
  1774. #ifdef __MSDOS__
  1775.         status = pid = commands[i].pid;
  1776. #else
  1777.     pid = wait (&status);
  1778. #endif
  1779.     if (pid < 0)
  1780.       abort ();
  1781.  
  1782.     if (status != 0)
  1783.       {
  1784.         int j;
  1785.         for (j = 0; j < n_commands; j++)
  1786.           if (commands[j].pid == pid)
  1787.         prog = commands[j].prog;
  1788.  
  1789.         if ((status & 0x7F) != 0)
  1790.           {
  1791.         fatal ("Internal compiler error: program %s got fatal signal %d",
  1792.                prog, (status & 0x7F));
  1793.         signal_count++;
  1794.           }
  1795.         if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  1796.           ret_code = -1;
  1797.       }
  1798.       }
  1799.     return ret_code;
  1800.   }
  1801. }
  1802.  
  1803. /* Find all the switches given to us
  1804.    and make a vector describing them.
  1805.    The elements of the vector are strings, one per switch given.
  1806.    If a switch uses following arguments, then the `part1' field
  1807.    is the switch itself and the `args' field
  1808.    is a null-terminated vector containing the following arguments.
  1809.    The `valid' field is nonzero if any spec has looked at this switch;
  1810.    if it remains zero at the end of the run, it must be meaningless.  */
  1811.  
  1812. struct switchstr
  1813. {
  1814.   char *part1;
  1815.   char **args;
  1816.   int valid;
  1817. };
  1818.  
  1819. static struct switchstr *switches;
  1820.  
  1821. static int n_switches;
  1822.  
  1823. struct infile
  1824. {
  1825.   char *name;
  1826.   char *language;
  1827. };
  1828.  
  1829. /* Also a vector of input files specified.  */
  1830.  
  1831. static struct infile *infiles;
  1832.  
  1833. static int n_infiles;
  1834.  
  1835. /* And a vector of corresponding output files is made up later.  */
  1836.  
  1837. static char **outfiles;
  1838.  
  1839. /* Create the vector `switches' and its contents.
  1840.    Store its length in `n_switches'.  */
  1841.  
  1842. static void
  1843. process_command (argc, argv)
  1844.      int argc;
  1845.      char **argv;
  1846. {
  1847.   register int i;
  1848.   char *temp;
  1849.   char *spec_lang = 0;
  1850.   int last_language_n_infiles;
  1851.  
  1852.   gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
  1853.  
  1854.   n_switches = 0;
  1855.   n_infiles = 0;
  1856.  
  1857.   /* Default for -V is our version number, ending at first space.  */
  1858.   spec_version = save_string (version_string, strlen (version_string));
  1859.   for (temp = spec_version; *temp && *temp != ' '; temp++);
  1860.   if (*temp) *temp = '\0';
  1861.  
  1862.   /* Set up the default search paths.  */
  1863.  
  1864.   if (gcc_exec_prefix)
  1865.     {
  1866.       add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
  1867.       add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
  1868.     }
  1869.  
  1870.   /* COMPILER_PATH and LIBRARY_PATH have values
  1871.      that are lists of directory names with colons.  */
  1872.  
  1873.   temp = getenv ("COMPILER_PATH");
  1874.   if (temp)
  1875.     {
  1876.       char *startp, *endp;
  1877.       char *nstore = (char *) alloca (strlen (temp) + 3);
  1878.  
  1879.       startp = endp = temp;
  1880.       while (1)
  1881.     {
  1882.       if (*endp == PATH_SEPARATOR || *endp == 0)
  1883.         {
  1884.           strncpy (nstore, startp, endp-startp);
  1885.           if (endp == startp)
  1886.         {
  1887.           strcpy (nstore, "./");
  1888.         }
  1889.           else if (endp[-1] != '/')
  1890.         {
  1891.           nstore[endp-startp] = '/';
  1892.           nstore[endp-startp+1] = 0;
  1893.         }
  1894.           else
  1895.         nstore[endp-startp] = 0;
  1896.           add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
  1897.           if (*endp == 0)
  1898.         break;
  1899.           endp = startp = endp + 1;
  1900.         }
  1901.       else
  1902.         endp++;
  1903.     }
  1904.     }
  1905.  
  1906.   temp = getenv ("LIBRARY_PATH");
  1907.   if (temp)
  1908.     {
  1909.       char *startp, *endp;
  1910.       char *nstore = (char *) alloca (strlen (temp) + 3);
  1911.  
  1912.       startp = endp = temp;
  1913.       while (1)
  1914.     {
  1915.       if (*endp == PATH_SEPARATOR || *endp == 0)
  1916.         {
  1917.           strncpy (nstore, startp, endp-startp);
  1918.           if (endp == startp)
  1919.         {
  1920.           strcpy (nstore, "./");
  1921.         }
  1922.           else if (endp[-1] != '/')
  1923.         {
  1924.           nstore[endp-startp] = '/';
  1925.           nstore[endp-startp+1] = 0;
  1926.         }
  1927.           else
  1928.         nstore[endp-startp] = 0;
  1929.           add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
  1930.           /* Make separate list of dirs that came from LIBRARY_PATH.  */
  1931.           add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
  1932.           if (*endp == 0)
  1933.         break;
  1934.           endp = startp = endp + 1;
  1935.         }
  1936.       else
  1937.         endp++;
  1938.     }
  1939.     }
  1940.  
  1941.   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
  1942.   temp = getenv ("LPATH");
  1943.   if (temp)
  1944.     {
  1945.       char *startp, *endp;
  1946.       char *nstore = (char *) alloca (strlen (temp) + 3);
  1947.  
  1948.       startp = endp = temp;
  1949.       while (1)
  1950.     {
  1951.       if (*endp == PATH_SEPARATOR || *endp == 0)
  1952.         {
  1953.           strncpy (nstore, startp, endp-startp);
  1954.           if (endp == startp)
  1955.         {
  1956.           strcpy (nstore, "./");
  1957.         }
  1958.           else if (endp[-1] != '/')
  1959.         {
  1960.           nstore[endp-startp] = '/';
  1961.           nstore[endp-startp+1] = 0;
  1962.         }
  1963.           else
  1964.         nstore[endp-startp] = 0;
  1965.           add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
  1966.           /* Make separate list of dirs that came from LIBRARY_PATH.  */
  1967.           add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
  1968.           if (*endp == 0)
  1969.         break;
  1970.           endp = startp = endp + 1;
  1971.         }
  1972.       else
  1973.         endp++;
  1974.     }
  1975.     }
  1976.  
  1977.   /* Scan argv twice.  Here, the first time, just count how many switches
  1978.      there will be in their vector, and how many input files in theirs.
  1979.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  1980.  
  1981.   for (i = 1; i < argc; i++)
  1982.     {
  1983.       if (! strcmp (argv[i], "-dumpspecs"))
  1984.     {
  1985.       printf ("*asm:\n%s\n\n", asm_spec);
  1986.       printf ("*asm_final:\n%s\n\n", asm_final_spec);
  1987.       printf ("*cpp:\n%s\n\n", cpp_spec);
  1988.       printf ("*cc1:\n%s\n\n", cc1_spec);
  1989.       printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
  1990.       printf ("*endfile:\n%s\n\n", endfile_spec);
  1991.       printf ("*link:\n%s\n\n", link_spec);
  1992.       printf ("*lib:\n%s\n\n", lib_spec);
  1993.       printf ("*startfile:\n%s\n\n", startfile_spec);
  1994.       printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
  1995.       printf ("*signed_char:\n%s\n\n", signed_char_spec);
  1996.       printf ("*predefines:\n%s\n\n", cpp_predefines);
  1997.       printf ("*cross_compile:\n%d\n\n", cross_compile);
  1998.  
  1999.       exit (0);
  2000.     }
  2001.       else if (! strcmp (argv[i], "-dumpversion"))
  2002.     {
  2003.       printf ("%s\n", version_string);
  2004.       exit (0);
  2005.     }
  2006.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2007.     {
  2008.       print_libgcc_file_name = 1;
  2009.     }
  2010.       else if (! strcmp (argv[i], "-Xlinker"))
  2011.     {
  2012.       /* Pass the argument of this option to the linker when we link.  */
  2013.  
  2014.       if (i + 1 == argc)
  2015.         fatal ("argument to `-Xlinker' is missing");
  2016.  
  2017.       n_linker_options++;
  2018.       if (!linker_options)
  2019.         linker_options
  2020.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  2021.       else
  2022.         linker_options
  2023.           = (char **) xrealloc (linker_options,
  2024.                     n_linker_options * sizeof (char **));
  2025.  
  2026.       linker_options[n_linker_options - 1] = argv[++i];
  2027.     }
  2028.       else if (! strncmp (argv[i], "-Wl,", 4))
  2029.     {
  2030.       int prev, j;
  2031.       /* Pass the rest of this option to the linker when we link.  */
  2032.  
  2033.       n_linker_options++;
  2034.       if (!linker_options)
  2035.         linker_options
  2036.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  2037.       else
  2038.         linker_options
  2039.           = (char **) xrealloc (linker_options,
  2040.                     n_linker_options * sizeof (char **));
  2041.  
  2042.       /* Split the argument at commas.  */
  2043.       prev = 4;
  2044.       for (j = 4; argv[i][j]; j++)
  2045.         if (argv[i][j] == ',')
  2046.           {
  2047.         linker_options[n_linker_options - 1]
  2048.           = save_string (argv[i] + prev, j - prev);
  2049.         n_linker_options++;
  2050.         linker_options
  2051.           = (char **) xrealloc (linker_options,
  2052.                     n_linker_options * sizeof (char **));
  2053.         prev = j + 1;
  2054.           }
  2055.       /* Record the part after the last comma.  */
  2056.       linker_options[n_linker_options - 1] = argv[i] + prev;
  2057.     }
  2058.       else if (! strncmp (argv[i], "-Wa,", 4))
  2059.     {
  2060.       int prev, j;
  2061.       /* Pass the rest of this option to the assembler.  */
  2062.  
  2063.       n_assembler_options++;
  2064.       if (!assembler_options)
  2065.         assembler_options
  2066.           = (char **) xmalloc (n_assembler_options * sizeof (char **));
  2067.       else
  2068.         assembler_options
  2069.           = (char **) xrealloc (assembler_options,
  2070.                     n_assembler_options * sizeof (char **));
  2071.  
  2072.       /* Split the argument at commas.  */
  2073.       prev = 4;
  2074.       for (j = 4; argv[i][j]; j++)
  2075.         if (argv[i][j] == ',')
  2076.           {
  2077.         assembler_options[n_assembler_options - 1]
  2078.           = save_string (argv[i] + prev, j - prev);
  2079.         n_assembler_options++;
  2080.         assembler_options
  2081.           = (char **) xrealloc (assembler_options,
  2082.                     n_assembler_options * sizeof (char **));
  2083.         prev = j + 1;
  2084.           }
  2085.       /* Record the part after the last comma.  */
  2086.       assembler_options[n_assembler_options - 1] = argv[i] + prev;
  2087.     }
  2088.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2089.     /* Compensate for the +e options to the C++ front-end.  */
  2090.     n_switches++;
  2091.       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
  2092.     {
  2093.       register char *p = &argv[i][1];
  2094.       register int c = *p;
  2095.  
  2096.       switch (c)
  2097.         {
  2098.         case 'b':
  2099.           if (p[1] == 0 && i + 1 == argc)
  2100.         fatal ("argument to `-b' is missing");
  2101.           if (p[1] == 0)
  2102.         spec_machine = argv[++i];
  2103.           else
  2104.         spec_machine = p + 1;
  2105.           break;
  2106.  
  2107.         case 'B':
  2108.           {
  2109.         int *temp = (int *) xmalloc (sizeof (int));
  2110.         char *value;
  2111.         if (p[1] == 0 && i + 1 == argc)
  2112.           fatal ("argument to `-B' is missing");
  2113.         if (p[1] == 0)
  2114.           value = argv[++i];
  2115.         else
  2116.           value = p + 1;
  2117.         add_prefix (&exec_prefix, value, 1, 0, temp);
  2118.         add_prefix (&startfile_prefix, value, 1, 0, temp);
  2119.           }
  2120.           break;
  2121.  
  2122.         case 'v':    /* Print our subcommands and print versions.  */
  2123.           n_switches++;
  2124.           /* If they do anything other than exactly `-v', don't set
  2125.          verbose_flag; rather, continue on to give the error.  */
  2126.           if (p[1] != 0)
  2127.         break;
  2128.           verbose_flag++;
  2129.           break;
  2130.  
  2131.         case 'V':
  2132.           if (p[1] == 0 && i + 1 == argc)
  2133.         fatal ("argument to `-V' is missing");
  2134.           if (p[1] == 0)
  2135.         spec_version = argv[++i];
  2136.           else
  2137.         spec_version = p + 1;
  2138.           break;
  2139.  
  2140.         case 's':
  2141.           if (!strcmp (p, "save-temps"))
  2142.         {
  2143.           save_temps_flag = 1;
  2144.           n_switches++;
  2145.           break;
  2146.         }
  2147.         default:
  2148.           n_switches++;
  2149.  
  2150.           if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2151.         i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2152.           else if (WORD_SWITCH_TAKES_ARG (p))
  2153.         i += WORD_SWITCH_TAKES_ARG (p);
  2154.         }
  2155.     }
  2156.       else
  2157.     n_infiles++;
  2158.     }
  2159.  
  2160.   /* Set up the search paths before we go looking for config files.  */
  2161.  
  2162.   /* These come before the md prefixes so that we will find gcc's subcommands
  2163.      (such as cpp) rather than those of the host system.  */
  2164.   /* Use 2 as fourth arg meaning try just the machine as a suffix,
  2165.      as well as trying the machine and the version.  */
  2166.   add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
  2167.   add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
  2168.  
  2169.   add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
  2170.   add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
  2171.  
  2172.   /* More prefixes are enabled in main, after we read the specs file
  2173.      and determine whether this is cross-compilation or not.  */
  2174.  
  2175.  
  2176.   /* Then create the space for the vectors and scan again.  */
  2177.  
  2178.   switches = ((struct switchstr *)
  2179.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  2180.   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
  2181.   n_switches = 0;
  2182.   n_infiles = 0;
  2183.   last_language_n_infiles = -1;
  2184.  
  2185.   /* This, time, copy the text of each switch and store a pointer
  2186.      to the copy in the vector of switches.
  2187.      Store all the infiles in their vector.  */
  2188.  
  2189.   for (i = 1; i < argc; i++)
  2190.     {
  2191.       /* Just skip the switches that were handled by the preceding loop.  */
  2192.       if (!strcmp (argv[i], "-Xlinker"))
  2193.     i++;
  2194.       else if (! strncmp (argv[i], "-Wl,", 4))
  2195.     ;
  2196.       else if (! strncmp (argv[i], "-Wa,", 4))
  2197.     ;
  2198.       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
  2199.     ;
  2200.       else if (argv[i][0] == '+' && argv[i][1] == 'e')
  2201.     {
  2202.       /* Compensate for the +e options to the C++ front-end;
  2203.          they're there simply for cfront call-compatability.  We do
  2204.          some magic in default_compilers to pass them down properly.
  2205.          Note we deliberately start at the `+' here, to avoid passing
  2206.          -e0 or -e1 down into the linker.  */
  2207.       switches[n_switches].part1 = &argv[i][0];
  2208.       switches[n_switches].args = 0;
  2209.       switches[n_switches].valid = 0;
  2210.       n_switches++;
  2211.     }
  2212.       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
  2213.     {
  2214.       register char *p = &argv[i][1];
  2215.       register int c = *p;
  2216.  
  2217.       if (c == 'B' || c == 'b' || c == 'V')
  2218.         {
  2219.           /* Skip a separate arg, if any.  */
  2220.           if (p[1] == 0)
  2221.         i++;
  2222.           continue;
  2223.         }
  2224.       if (c == 'x')
  2225.         {
  2226.           if (p[1] == 0 && i + 1 == argc)
  2227.         fatal ("argument to `-x' is missing");
  2228.           if (p[1] == 0)
  2229.         spec_lang = argv[++i];
  2230.           else
  2231.         spec_lang = p + 1;
  2232.           if (! strcmp (spec_lang, "none"))
  2233.         /* Suppress the warning if -xnone comes after the last input file,
  2234.            because alternate command interfaces like g++ might find it
  2235.            useful to place -xnone after each input file.  */
  2236.         spec_lang = 0;
  2237.           else
  2238.         last_language_n_infiles = n_infiles;
  2239.           continue;
  2240.         }
  2241.       switches[n_switches].part1 = p;
  2242.       /* Deal with option arguments in separate argv elements.  */
  2243.       if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
  2244.           || WORD_SWITCH_TAKES_ARG (p)) {
  2245.         int j = 0;
  2246.         int n_args = WORD_SWITCH_TAKES_ARG (p);
  2247.  
  2248.         if (n_args == 0) {
  2249.           /* Count only the option arguments in separate argv elements.  */
  2250.           n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
  2251.         }
  2252.         if (i + n_args >= argc)
  2253.           fatal ("argument to `-%s' is missing", p);
  2254.         switches[n_switches].args
  2255.           = (char **) xmalloc ((n_args + 1) * sizeof (char *));
  2256.         while (j < n_args)
  2257.           switches[n_switches].args[j++] = argv[++i];
  2258.         /* Null-terminate the vector.  */
  2259.         switches[n_switches].args[j] = 0;
  2260.       } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
  2261.         /* On some systems, ld cannot handle -o or -L without space.
  2262.            So split the -o or -L from its argument.  */
  2263.         switches[n_switches].part1 = (c == 'o' ? "o" : "L");
  2264.         switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
  2265.         switches[n_switches].args[0] = xmalloc (strlen (p));
  2266.         strcpy (switches[n_switches].args[0], &p[1]);
  2267.         switches[n_switches].args[1] = 0;
  2268.       } else
  2269.         switches[n_switches].args = 0;
  2270.       switches[n_switches].valid = 0;
  2271.       /* This is always valid, since gcc.c itself understands it.  */
  2272.       if (!strcmp (p, "save-temps"))
  2273.         switches[n_switches].valid = 1;
  2274.       n_switches++;
  2275.     }
  2276.       else
  2277.     {
  2278.       infiles[n_infiles].language = spec_lang;
  2279.       infiles[n_infiles++].name = argv[i];
  2280.     }
  2281.     }
  2282.  
  2283.   if (n_infiles == last_language_n_infiles)
  2284.     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
  2285.  
  2286.   switches[n_switches].part1 = 0;
  2287.   infiles[n_infiles].name = 0;
  2288.  
  2289.   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
  2290.   if (gcc_exec_prefix)
  2291.     {
  2292.       temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
  2293.                    + strlen (spec_machine) + 3);
  2294.       strcpy (temp, gcc_exec_prefix);
  2295.       strcat (temp, spec_machine);
  2296.       strcat (temp, "/");
  2297.       strcat (temp, spec_version);
  2298.       strcat (temp, "/");
  2299.       gcc_exec_prefix = temp;
  2300.     }
  2301. }
  2302.  
  2303. /* Process a spec string, accumulating and running commands.  */
  2304.  
  2305. /* These variables describe the input file name.
  2306.    input_file_number is the index on outfiles of this file,
  2307.    so that the output file name can be stored for later use by %o.
  2308.    input_basename is the start of the part of the input file
  2309.    sans all directory names, and basename_length is the number
  2310.    of characters starting there excluding the suffix .c or whatever.  */
  2311.  
  2312. static char *input_filename;
  2313. static int input_file_number;
  2314. static int input_filename_length;
  2315. static int basename_length;
  2316. static char *input_basename;
  2317. static char *input_suffix;
  2318.  
  2319. /* These are variables used within do_spec and do_spec_1.  */
  2320.  
  2321. /* Nonzero if an arg has been started and not yet terminated
  2322.    (with space, tab or newline).  */
  2323. static int arg_going;
  2324.  
  2325. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  2326.    is a temporary file name.  */
  2327. static int delete_this_arg;
  2328.  
  2329. /* Nonzero means %w has been seen; the next arg to be terminated
  2330.    is the output file name of this compilation.  */
  2331. static int this_is_output_file;
  2332.  
  2333. /* Nonzero means %s has been seen; the next arg to be terminated
  2334.    is the name of a library file and we should try the standard
  2335.    search dirs for it.  */
  2336. static int this_is_library_file;
  2337.  
  2338. /* Process the spec SPEC and run the commands specified therein.
  2339.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  2340.  
  2341. static int
  2342. do_spec (spec)
  2343.      char *spec;
  2344. {
  2345.   int value;
  2346.  
  2347.   clear_args ();
  2348.   arg_going = 0;
  2349.   delete_this_arg = 0;
  2350.   this_is_output_file = 0;
  2351.   this_is_library_file = 0;
  2352.  
  2353.   value = do_spec_1 (spec, 0, NULL_PTR);
  2354.  
  2355.   /* Force out any unfinished command.
  2356.      If -pipe, this forces out the last command if it ended in `|'.  */
  2357.   if (value == 0)
  2358.     {
  2359.       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  2360.     argbuf_index--;
  2361.  
  2362.       if (argbuf_index > 0)
  2363.     value = execute ();
  2364.     }
  2365.  
  2366.   return value;
  2367. }
  2368.  
  2369. /* Process the sub-spec SPEC as a portion of a larger spec.
  2370.    This is like processing a whole spec except that we do
  2371.    not initialize at the beginning and we do not supply a
  2372.    newline by default at the end.
  2373.    INSWITCH nonzero means don't process %-sequences in SPEC;
  2374.    in this case, % is treated as an ordinary character.
  2375.    This is used while substituting switches.
  2376.    INSWITCH nonzero also causes SPC not to terminate an argument.
  2377.  
  2378.    Value is zero unless a line was finished
  2379.    and the command on that line reported an error.  */
  2380.  
  2381. static int
  2382. do_spec_1 (spec, inswitch, soft_matched_part)
  2383.      char *spec;
  2384.      int inswitch;
  2385.      char *soft_matched_part;
  2386. {
  2387.   register char *p = spec;
  2388.   register int c;
  2389.   int i;
  2390.   char *string;
  2391.  
  2392.   while (c = *p++)
  2393.     /* If substituting a switch, treat all chars like letters.
  2394.        Otherwise, NL, SPC, TAB and % are special.  */
  2395.     switch (inswitch ? 'a' : c)
  2396.       {
  2397.       case '\n':
  2398.     /* End of line: finish any pending argument,
  2399.        then run the pending command if one has been started.  */
  2400.     if (arg_going)
  2401.       {
  2402.         obstack_1grow (&obstack, 0);
  2403.         string = obstack_finish (&obstack);
  2404.         if (this_is_library_file)
  2405.           string = find_file (string);
  2406.         store_arg (string, delete_this_arg, this_is_output_file);
  2407.         if (this_is_output_file)
  2408.           outfiles[input_file_number] = string;
  2409.       }
  2410.     arg_going = 0;
  2411.  
  2412.     if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
  2413.       {
  2414.         int i;
  2415.         for (i = 0; i < n_switches; i++)
  2416.           if (!strcmp (switches[i].part1, "pipe"))
  2417.         break;
  2418.  
  2419.         /* A `|' before the newline means use a pipe here,
  2420.            but only if -pipe was specified.
  2421.            Otherwise, execute now and don't pass the `|' as an arg.  */
  2422.         if (i < n_switches)
  2423.           {
  2424.         switches[i].valid = 1;
  2425.         break;
  2426.           }
  2427.         else
  2428.           argbuf_index--;
  2429.       }
  2430.  
  2431.     if (argbuf_index > 0)
  2432.       {
  2433.         int value = execute ();
  2434.         if (value)
  2435.           return value;
  2436.       }
  2437.     /* Reinitialize for a new command, and for a new argument.  */
  2438.     clear_args ();
  2439.     arg_going = 0;
  2440.     delete_this_arg = 0;
  2441.     this_is_output_file = 0;
  2442.     this_is_library_file = 0;
  2443.     break;
  2444.  
  2445.       case '|':
  2446.     /* End any pending argument.  */
  2447.     if (arg_going)
  2448.       {
  2449.         obstack_1grow (&obstack, 0);
  2450.         string = obstack_finish (&obstack);
  2451.         if (this_is_library_file)
  2452.           string = find_file (string);
  2453.         store_arg (string, delete_this_arg, this_is_output_file);
  2454.         if (this_is_output_file)
  2455.           outfiles[input_file_number] = string;
  2456.       }
  2457.  
  2458.     /* Use pipe */
  2459.     obstack_1grow (&obstack, c);
  2460.     arg_going = 1;
  2461.     break;
  2462.  
  2463.       case '\t':
  2464.       case ' ':
  2465.     /* Space or tab ends an argument if one is pending.  */
  2466.     if (arg_going)
  2467.       {
  2468.         obstack_1grow (&obstack, 0);
  2469.         string = obstack_finish (&obstack);
  2470.         if (this_is_library_file)
  2471.           string = find_file (string);
  2472.         store_arg (string, delete_this_arg, this_is_output_file);
  2473.         if (this_is_output_file)
  2474.           outfiles[input_file_number] = string;
  2475.       }
  2476.     /* Reinitialize for a new argument.  */
  2477.     arg_going = 0;
  2478.     delete_this_arg = 0;
  2479.     this_is_output_file = 0;
  2480.     this_is_library_file = 0;
  2481.     break;
  2482.  
  2483.       case '%':
  2484.     switch (c = *p++)
  2485.       {
  2486.       case 0:
  2487.         fatal ("Invalid specification!  Bug in cc.");
  2488.  
  2489.       case 'b':
  2490.         obstack_grow (&obstack, input_basename, basename_length);
  2491.         arg_going = 1;
  2492.         break;
  2493.  
  2494.       case 'd':
  2495.         delete_this_arg = 2;
  2496.         break;
  2497.  
  2498.       /* Dump out the directories specified with LIBRARY_PATH,
  2499.          followed by the absolute directories
  2500.          that we search for startfiles.  */
  2501.       case 'D':
  2502.         for (i = 0; i < 2; i++)
  2503.           {
  2504.         struct prefix_list *pl
  2505.           = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
  2506.         int bufsize = 100;
  2507.         char *buffer = (char *) xmalloc (bufsize);
  2508.         int idx;
  2509.  
  2510.         for (; pl; pl = pl->next)
  2511.           {
  2512. #ifdef RELATIVE_PREFIX_NOT_LINKDIR
  2513.             /* Used on systems which record the specified -L dirs
  2514.                and use them to search for dynamic linking.  */
  2515.             /* Relative directories always come from -B,
  2516.                and it is better not to use them for searching
  2517.                at run time.  In particular, stage1 loses  */
  2518.             if (pl->prefix[0] != '/')
  2519.               continue;
  2520. #endif
  2521.             if (machine_suffix)
  2522.               {
  2523.             if (is_linker_dir (pl->prefix, machine_suffix))
  2524.               {
  2525.                 do_spec_1 ("-L", 0, NULL_PTR);
  2526. #ifdef SPACE_AFTER_L_OPTION
  2527.                 do_spec_1 (" ", 0, NULL_PTR);
  2528. #endif
  2529.                 do_spec_1 (pl->prefix, 1, NULL_PTR);
  2530.                 /* Remove slash from machine_suffix.  */
  2531.                 if (strlen (machine_suffix) >= bufsize)
  2532.                   bufsize = strlen (machine_suffix) * 2 + 1;
  2533.                 buffer = (char *) xrealloc (buffer, bufsize);
  2534.                 strcpy (buffer, machine_suffix);
  2535.                 idx = strlen (buffer);
  2536.                 if (buffer[idx - 1] == '/')
  2537.                   buffer[idx - 1] = 0;
  2538.                 do_spec_1 (buffer, 1, NULL_PTR);
  2539.                 /* Make this a separate argument.  */
  2540.                 do_spec_1 (" ", 0, NULL_PTR);
  2541.               }
  2542.               }
  2543.             if (!pl->require_machine_suffix)
  2544.               {
  2545.             if (is_linker_dir (pl->prefix, ""))
  2546.               {
  2547.                 do_spec_1 ("-L", 0, NULL_PTR);
  2548. #ifdef SPACE_AFTER_L_OPTION
  2549.                 do_spec_1 (" ", 0, NULL_PTR);
  2550. #endif
  2551.                 /* Remove slash from pl->prefix.  */
  2552.                 if (strlen (pl->prefix) >= bufsize)
  2553.                   bufsize = strlen (pl->prefix) * 2 + 1;
  2554.                 buffer = (char *) xrealloc (buffer, bufsize);
  2555.                 strcpy (buffer, pl->prefix);
  2556.                 idx = strlen (buffer);
  2557.                 if (buffer[idx - 1] == '/')
  2558.                   buffer[idx - 1] = 0;
  2559.                 do_spec_1 (buffer, 1, NULL_PTR);
  2560.                 /* Make this a separate argument.  */
  2561.                 do_spec_1 (" ", 0, NULL_PTR);
  2562.               }
  2563.               }
  2564.           }
  2565.         free (buffer);
  2566.           }
  2567.         break;
  2568.  
  2569.       case 'e':
  2570.         /* {...:%efoo} means report an error with `foo' as error message
  2571.            and don't execute any more commands for this file.  */
  2572.         {
  2573.           char *q = p;
  2574.           char *buf;
  2575.           while (*p != 0 && *p != '\n') p++;
  2576.           buf = (char *) alloca (p - q + 1);
  2577.           strncpy (buf, q, p - q);
  2578.           buf[p - q] = 0;
  2579.           error ("%s", buf);
  2580.           return -1;
  2581.         }
  2582.         break;
  2583.  
  2584.       case 'g':
  2585.       case 'u':
  2586.       case 'U':
  2587.         if (save_temps_flag)
  2588.           obstack_grow (&obstack, input_basename, basename_length);
  2589.         else
  2590.           {
  2591. #ifdef MKTEMP_EACH_FILE
  2592.         /* ??? This has a problem: the total number of
  2593.            values mktemp can return is limited.
  2594.            That matters for the names of object files.
  2595.            In 2.4, do something about that.  */
  2596.         struct temp_name *t;
  2597.         char *suffix = p;
  2598.         while (*p == '.' || isalpha (*p))
  2599.           p++;
  2600.  
  2601.         /* See if we already have an association of %g/%u/%U and
  2602.            suffix.  */
  2603.         for (t = temp_names; t; t = t->next)
  2604.           if (t->length == p - suffix
  2605.               && strncmp (t->suffix, suffix, p - suffix) == 0
  2606.               && t->unique == (c != 'g'))
  2607.             break;
  2608.  
  2609.         /* Make a new association if needed.  %u requires one.  */
  2610.         if (t == 0 || c == 'u')
  2611.           {
  2612.             if (t == 0)
  2613.               {
  2614.             t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
  2615.             t->next = temp_names;
  2616.             temp_names = t;
  2617.               }
  2618.             t->length = p - suffix;
  2619.             t->suffix = save_string (suffix, p - suffix);
  2620.             t->unique = (c != 'g');
  2621.             choose_temp_base ();
  2622.             t->filename = temp_filename;
  2623.             t->filename_length = temp_filename_length;
  2624.           }
  2625.  
  2626.         obstack_grow (&obstack, t->filename, t->filename_length);
  2627.         delete_this_arg = 1;
  2628. #else
  2629.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  2630.         if (c == 'u' || c == 'U')
  2631.           {
  2632.             static int unique;
  2633.             char buff[9];
  2634.             if (c == 'u')
  2635.               unique++;
  2636.             sprintf (buff, "%d", unique);
  2637.             obstack_grow (&obstack, buff, strlen (buff));
  2638.           }
  2639. #endif
  2640.         delete_this_arg = 1;
  2641.           }
  2642.         arg_going = 1;
  2643.         break;
  2644.  
  2645.       case 'i':
  2646.         obstack_grow (&obstack, input_filename, input_filename_length);
  2647.         arg_going = 1;
  2648.         break;
  2649.  
  2650.       case 'I':
  2651.         if (gcc_exec_prefix)
  2652.           {
  2653.         do_spec_1 ("-iprefix", 1, NULL_PTR);
  2654.         /* Make this a separate argument.  */
  2655.         do_spec_1 (" ", 0, NULL_PTR);
  2656.         do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
  2657.         do_spec_1 (" ", 0, NULL_PTR);
  2658.           }
  2659.         break;
  2660.  
  2661.       case 'o':
  2662.         {
  2663.           register int f;
  2664.           for (f = 0; f < n_infiles; f++)
  2665.         store_arg (outfiles[f], 0, 0);
  2666.         }
  2667.         break;
  2668.  
  2669.       case 's':
  2670.         this_is_library_file = 1;
  2671.         break;
  2672.  
  2673.       case 'w':
  2674.         this_is_output_file = 1;
  2675.         break;
  2676.  
  2677.       case 'W':
  2678.         {
  2679.           int index = argbuf_index;
  2680.           /* Handle the {...} following the %W.  */
  2681.           if (*p != '{')
  2682.         abort ();
  2683.           p = handle_braces (p + 1);
  2684.           if (p == 0)
  2685.         return -1;
  2686.           /* If any args were output, mark the last one for deletion
  2687.          on failure.  */
  2688.           if (argbuf_index != index)
  2689.         record_temp_file (argbuf[argbuf_index - 1], 0, 1);
  2690.           break;
  2691.         }
  2692.  
  2693.       /* %x{OPTION} records OPTION for %X to output.  */
  2694.       case 'x':
  2695.         {
  2696.           char *p1 = p;
  2697.           char *string;
  2698.  
  2699.           /* Skip past the option value and make a copy.  */
  2700.           if (*p != '{')
  2701.         abort ();
  2702.           while (*p++ != '}')
  2703.         ;
  2704.           string = save_string (p1 + 1, p - p1 - 2);
  2705.  
  2706.           /* See if we already recorded this option.  */
  2707.           for (i = 0; i < n_linker_options; i++)
  2708.         if (! strcmp (string, linker_options[i]))
  2709.           {
  2710.             free (string);
  2711.             return 0;
  2712.           }
  2713.  
  2714.           /* This option is new; add it.  */
  2715.           n_linker_options++;
  2716.           if (!linker_options)
  2717.         linker_options
  2718.           = (char **) xmalloc (n_linker_options * sizeof (char **));
  2719.           else
  2720.         linker_options
  2721.           = (char **) xrealloc (linker_options,
  2722.                     n_linker_options * sizeof (char **));
  2723.  
  2724.           linker_options[n_linker_options - 1] = string;
  2725.         }
  2726.         break;
  2727.  
  2728.       /* Dump out the options accumulated previously using %x,
  2729.          -Xlinker and -Wl,.  */
  2730.       case 'X':
  2731.         for (i = 0; i < n_linker_options; i++)
  2732.           {
  2733.         do_spec_1 (linker_options[i], 1, NULL_PTR);
  2734.         /* Make each accumulated option a separate argument.  */
  2735.         do_spec_1 (" ", 0, NULL_PTR);
  2736.           }
  2737.         break;
  2738.  
  2739.       /* Dump out the options accumulated previously using -Wa,.  */
  2740.       case 'Y':
  2741.         for (i = 0; i < n_assembler_options; i++)
  2742.           {
  2743.         do_spec_1 (assembler_options[i], 1, NULL_PTR);
  2744.         /* Make each accumulated option a separate argument.  */
  2745.         do_spec_1 (" ", 0, NULL_PTR);
  2746.           }
  2747.         break;
  2748.  
  2749.         /* Here are digits and numbers that just process
  2750.            a certain constant string as a spec.
  2751.         /* Here are digits and numbers that just process
  2752.            a certain constant string as a spec.  */
  2753.  
  2754.       case '1':
  2755.         do_spec_1 (cc1_spec, 0, NULL_PTR);
  2756.         break;
  2757.  
  2758.       case '2':
  2759.         do_spec_1 (cc1plus_spec, 0, NULL_PTR);
  2760.         break;
  2761.  
  2762.       case 'a':
  2763.         do_spec_1 (asm_spec, 0, NULL_PTR);
  2764.         break;
  2765.  
  2766.       case 'A':
  2767.         do_spec_1 (asm_final_spec, 0, NULL_PTR);
  2768.         break;
  2769.  
  2770.       case 'c':
  2771.         do_spec_1 (signed_char_spec, 0, NULL_PTR);
  2772.         break;
  2773.  
  2774.       case 'C':
  2775.         do_spec_1 (cpp_spec, 0, NULL_PTR);
  2776.         break;
  2777.  
  2778.       case 'E':
  2779.         do_spec_1 (endfile_spec, 0, NULL_PTR);
  2780.         break;
  2781.  
  2782.       case 'l':
  2783.         do_spec_1 (link_spec, 0, NULL_PTR);
  2784.         break;
  2785.  
  2786.       case 'L':
  2787.         do_spec_1 (lib_spec, 0, NULL_PTR);
  2788.         break;
  2789.  
  2790.       case 'p':
  2791.         {
  2792.           char *x = (char *) alloca (strlen (cpp_predefines) + 1);
  2793.           char *buf = x;
  2794.           char *y;
  2795.  
  2796.           /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
  2797.           y = cpp_predefines;
  2798.           while (*y != 0)
  2799.         {
  2800.           if (! strncmp (y, "-D", 2))
  2801.             /* Copy the whole option.  */
  2802.             while (*y && *y != ' ' && *y != '\t')
  2803.               *x++ = *y++;
  2804.           else if (*y == ' ' || *y == '\t')
  2805.             /* Copy whitespace to the result.  */
  2806.             *x++ = *y++;
  2807.           /* Don't copy other options.  */
  2808.           else
  2809.             y++;
  2810.         }
  2811.  
  2812.           *x = 0;
  2813.  
  2814.           do_spec_1 (buf, 0, NULL_PTR);
  2815.         }
  2816.         break;
  2817.  
  2818.       case 'P':
  2819.         {
  2820.           char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
  2821.           char *buf = x;
  2822.           char *y;
  2823.  
  2824.           /* Copy all of CPP_PREDEFINES into BUF,
  2825.          but put __ after every -D and at the end of each arg.  */
  2826.           y = cpp_predefines;
  2827.           while (*y != 0)
  2828.         {
  2829.           if (! strncmp (y, "-D", 2))
  2830.             {
  2831.               int flag = 0;
  2832.  
  2833.               *x++ = *y++;
  2834.               *x++ = *y++;
  2835.  
  2836.               if (strncmp (y, "__", 2))
  2837.                 {
  2838.               /* Stick __ at front of macro name.  */
  2839.               *x++ = '_';
  2840.               *x++ = '_';
  2841.               /* Arrange to stick __ at the end as well.  */
  2842.               flag = 1;
  2843.             }
  2844.  
  2845.               /* Copy the macro name.  */
  2846.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  2847.             *x++ = *y++;
  2848.  
  2849.               if (flag)
  2850.                 {
  2851.               *x++ = '_';
  2852.               *x++ = '_';
  2853.             }
  2854.  
  2855.               /* Copy the value given, if any.  */
  2856.               while (*y && *y != ' ' && *y != '\t')
  2857.             *x++ = *y++;
  2858.             }
  2859.           else if (*y == ' ' || *y == '\t')
  2860.             /* Copy whitespace to the result.  */
  2861.             *x++ = *y++;
  2862.           /* Don't copy -A options  */
  2863.           else
  2864.             y++;
  2865.         }
  2866.           *x++ = ' ';
  2867.  
  2868.           /* Copy all of CPP_PREDEFINES into BUF,
  2869.          but put __ after every -D.  */
  2870.           y = cpp_predefines;
  2871.           while (*y != 0)
  2872.         {
  2873.           if (! strncmp (y, "-D", 2))
  2874.             {
  2875.               *x++ = *y++;
  2876.               *x++ = *y++;
  2877.  
  2878.               if (strncmp (y, "__", 2))
  2879.                 {
  2880.               /* Stick __ at front of macro name.  */
  2881.               *x++ = '_';
  2882.               *x++ = '_';
  2883.             }
  2884.  
  2885.               /* Copy the macro name.  */
  2886.               while (*y && *y != '=' && *y != ' ' && *y != '\t')
  2887.             *x++ = *y++;
  2888.  
  2889.               /* Copy the value given, if any.  */
  2890.               while (*y && *y != ' ' && *y != '\t')
  2891.             *x++ = *y++;
  2892.             }
  2893.           else if (*y == ' ' || *y == '\t')
  2894.             /* Copy whitespace to the result.  */
  2895.             *x++ = *y++;
  2896.           /* Don't copy -A options  */
  2897.           else
  2898.             y++;
  2899.         }
  2900.           *x++ = ' ';
  2901.  
  2902.           /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
  2903.           y = cpp_predefines;
  2904.           while (*y != 0)
  2905.         {
  2906.           if (! strncmp (y, "-A", 2))
  2907.             /* Copy the whole option.  */
  2908.             while (*y && *y != ' ' && *y != '\t')
  2909.               *x++ = *y++;
  2910.           else if (*y == ' ' || *y == '\t')
  2911.             /* Copy whitespace to the result.  */
  2912.             *x++ = *y++;
  2913.           /* Don't copy other options.  */
  2914.           else
  2915.             y++;
  2916.         }
  2917.  
  2918.           *x = 0;
  2919.  
  2920.           do_spec_1 (buf, 0, NULL_PTR);
  2921.         }
  2922.         break;
  2923.  
  2924.       case 'S':
  2925.         do_spec_1 (startfile_spec, 0, NULL_PTR);
  2926.         break;
  2927.  
  2928.         /* Here we define characters other than letters and digits.  */
  2929.  
  2930.       case '{':
  2931.         p = handle_braces (p);
  2932.         if (p == 0)
  2933.           return -1;
  2934.         break;
  2935.  
  2936.       case '%':
  2937.         obstack_1grow (&obstack, '%');
  2938.         break;
  2939.  
  2940.       case '*':
  2941.         do_spec_1 (soft_matched_part, 1, NULL_PTR);
  2942.         do_spec_1 (" ", 0, NULL_PTR);
  2943.         break;
  2944.  
  2945.         /* Process a string found as the value of a spec given by name.
  2946.            This feature allows individual machine descriptions
  2947.            to add and use their own specs.
  2948.            %[...] modifies -D options the way %P does;
  2949.            %(...) uses the spec unmodified.  */
  2950.       case '(':
  2951.       case '[':
  2952.         {
  2953.           char *name = p;
  2954.           struct spec_list *sl;
  2955.           int len;
  2956.  
  2957.           /* The string after the S/P is the name of a spec that is to be
  2958.          processed. */
  2959.           while (*p && *p != ')' && *p != ']')
  2960.         p++;
  2961.  
  2962.           /* See if it's in the list */
  2963.           for (len = p - name, sl = specs; sl; sl = sl->next)
  2964.         if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
  2965.           {
  2966.             name = sl->spec;
  2967.             break;
  2968.           }
  2969.  
  2970.           if (sl)
  2971.         {
  2972.           if (c == '(')
  2973.             do_spec_1 (name, 0, NULL_PTR);
  2974.           else
  2975.             {
  2976.               char *x = (char *) alloca (strlen (name) * 2 + 1);
  2977.               char *buf = x;
  2978.               char *y = name;
  2979.  
  2980.               /* Copy all of NAME into BUF, but put __ after
  2981.              every -D and at the end of each arg,  */
  2982.               while (1)
  2983.             {
  2984.               if (! strncmp (y, "-D", 2))
  2985.                 {
  2986.                   *x++ = '-';
  2987.                   *x++ = 'D';
  2988.                   *x++ = '_';
  2989.                   *x++ = '_';
  2990.                   y += 2;
  2991.                 }
  2992.               else if (*y == ' ' || *y == 0)
  2993.                 {
  2994.                   *x++ = '_';
  2995.                   *x++ = '_';
  2996.                   if (*y == 0)
  2997.                 break;
  2998.                   else
  2999.                 *x++ = *y++;
  3000.                 }
  3001.               else
  3002.                 *x++ = *y++;
  3003.             }
  3004.               *x = 0;
  3005.  
  3006.               do_spec_1 (buf, 0, NULL_PTR);
  3007.             }
  3008.         }
  3009.  
  3010.           /* Discard the closing paren or bracket.  */
  3011.           if (*p)
  3012.         p++;
  3013.         }
  3014.         break;
  3015.  
  3016.       default:
  3017.         abort ();
  3018.       }
  3019.     break;
  3020.  
  3021.       case '\\':
  3022.     /* Backslash: treat next character as ordinary.  */
  3023.     c = *p++;
  3024.  
  3025.     /* fall through */
  3026.       default:
  3027.     /* Ordinary character: put it into the current argument.  */
  3028.     obstack_1grow (&obstack, c);
  3029.     arg_going = 1;
  3030.       }
  3031.  
  3032.   return 0;        /* End of string */
  3033. }
  3034.  
  3035. /* Return 0 if we call do_spec_1 and that returns -1.  */
  3036.  
  3037. static char *
  3038. handle_braces (p)
  3039.      register char *p;
  3040. {
  3041.   register char *q;
  3042.   char *filter;
  3043.   int pipe = 0;
  3044.   int negate = 0;
  3045.   int suffix = 0;
  3046.  
  3047.   if (*p == '|')
  3048.     /* A `|' after the open-brace means,
  3049.        if the test fails, output a single minus sign rather than nothing.
  3050.        This is used in %{|!pipe:...}.  */
  3051.     pipe = 1, ++p;
  3052.  
  3053.   if (*p == '!')
  3054.     /* A `!' after the open-brace negates the condition:
  3055.        succeed if the specified switch is not present.  */
  3056.     negate = 1, ++p;
  3057.  
  3058.   if (*p == '.')
  3059.     /* A `.' after the open-brace means test against the current suffix.  */
  3060.     {
  3061.       if (pipe)
  3062.     abort ();
  3063.  
  3064.       suffix = 1;
  3065.       ++p;
  3066.     }
  3067.  
  3068.   filter = p;
  3069.   while (*p != ':' && *p != '}') p++;
  3070.   if (*p != '}')
  3071.     {
  3072.       register int count = 1;
  3073.       q = p + 1;
  3074.       while (count > 0)
  3075.     {
  3076.       if (*q == '{')
  3077.         count++;
  3078.       else if (*q == '}')
  3079.         count--;
  3080.       else if (*q == 0)
  3081.         abort ();
  3082.       q++;
  3083.     }
  3084.     }
  3085.   else
  3086.     q = p + 1;
  3087.  
  3088.   if (suffix)
  3089.     {
  3090.       int found = (input_suffix != 0
  3091.            && strlen (input_suffix) == p - filter
  3092.            && strncmp (input_suffix, filter, p - filter) == 0);
  3093.  
  3094.       if (p[0] == '}')
  3095.     abort ();
  3096.  
  3097.       if (negate != found
  3098.       && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  3099.     return 0;
  3100.  
  3101.       return q;
  3102.     }
  3103.   else if (p[-1] == '*' && p[0] == '}')
  3104.     {
  3105.       /* Substitute all matching switches as separate args.  */
  3106.       register int i;
  3107.       --p;
  3108.       for (i = 0; i < n_switches; i++)
  3109.     if (!strncmp (switches[i].part1, filter, p - filter))
  3110.       give_switch (i, 0);
  3111.     }
  3112.   else
  3113.     {
  3114.       /* Test for presence of the specified switch.  */
  3115.       register int i;
  3116.       int present = 0;
  3117.  
  3118.       /* If name specified ends in *, as in {x*:...},
  3119.      check for %* and handle that case.  */
  3120.       if (p[-1] == '*' && !negate)
  3121.     {
  3122.       int substitution;
  3123.       char *r = p;
  3124.  
  3125.       /* First see whether we have %*.  */
  3126.       substitution = 0;
  3127.       while (r < q)
  3128.         {
  3129.           if (*r == '%' && r[1] == '*')
  3130.         substitution = 1;
  3131.           r++;
  3132.         }
  3133.       /* If we do, handle that case.  */
  3134.       if (substitution)
  3135.         {
  3136.           /* Substitute all matching switches as separate args.
  3137.          But do this by substituting for %*
  3138.          in the text that follows the colon.  */
  3139.  
  3140.           unsigned hard_match_len = p - filter - 1;
  3141.           char *string = save_string (p + 1, q - p - 2);
  3142.  
  3143.           for (i = 0; i < n_switches; i++)
  3144.         if (!strncmp (switches[i].part1, filter, hard_match_len))
  3145.           {
  3146.             do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
  3147.             /* Pass any arguments this switch has.  */
  3148.             give_switch (i, 1);
  3149.           }
  3150.  
  3151.           return q;
  3152.         }
  3153.     }
  3154.  
  3155.       /* If name specified ends in *, as in {x*:...},
  3156.      check for presence of any switch name starting with x.  */
  3157.       if (p[-1] == '*')
  3158.     {
  3159.       for (i = 0; i < n_switches; i++)
  3160.         {
  3161.           unsigned hard_match_len = p - filter - 1;
  3162.  
  3163.           if (!strncmp (switches[i].part1, filter, hard_match_len))
  3164.         {
  3165.           switches[i].valid = 1;
  3166.           present = 1;
  3167.         }
  3168.         }
  3169.     }
  3170.       /* Otherwise, check for presence of exact name specified.  */
  3171.       else
  3172.     {
  3173.       for (i = 0; i < n_switches; i++)
  3174.         {
  3175.           if (!strncmp (switches[i].part1, filter, p - filter)
  3176.           && switches[i].part1[p - filter] == 0)
  3177.         {
  3178.           switches[i].valid = 1;
  3179.           present = 1;
  3180.           break;
  3181.         }
  3182.         }
  3183.     }
  3184.  
  3185.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  3186.      then substitute either the switch or the specified
  3187.      conditional text.  */
  3188.       if (present != negate)
  3189.     {
  3190.       if (*p == '}')
  3191.         {
  3192.           give_switch (i, 0);
  3193.         }
  3194.       else
  3195.         {
  3196.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
  3197.         return 0;
  3198.         }
  3199.     }
  3200.       else if (pipe)
  3201.     {
  3202.       /* Here if a %{|...} conditional fails: output a minus sign,
  3203.          which means "standard output" or "standard input".  */
  3204.       do_spec_1 ("-", 0, NULL_PTR);
  3205.     }
  3206.     }
  3207.  
  3208.   return q;
  3209. }
  3210.  
  3211. /* Pass a switch to the current accumulating command
  3212.    in the same form that we received it.
  3213.    SWITCHNUM identifies the switch; it is an index into
  3214.    the vector of switches gcc received, which is `switches'.
  3215.    This cannot fail since it never finishes a command line.
  3216.  
  3217.    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.  */
  3218.  
  3219. static void
  3220. give_switch (switchnum, omit_first_word)
  3221.      int switchnum;
  3222.      int omit_first_word;
  3223. {
  3224.   if (!omit_first_word)
  3225.     {
  3226.       do_spec_1 ("-", 0, NULL_PTR);
  3227.       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
  3228.     }
  3229.   do_spec_1 (" ", 0, NULL_PTR);
  3230.   if (switches[switchnum].args != 0)
  3231.     {
  3232.       char **p;
  3233.       for (p = switches[switchnum].args; *p; p++)
  3234.     {
  3235.       do_spec_1 (*p, 1, NULL_PTR);
  3236.       do_spec_1 (" ", 0, NULL_PTR);
  3237.     }
  3238.     }
  3239.   switches[switchnum].valid = 1;
  3240. }
  3241.  
  3242. /* Search for a file named NAME trying various prefixes including the
  3243.    user's -B prefix and some standard ones.
  3244.    Return the absolute file name found.  If nothing is found, return NAME.  */
  3245.  
  3246. static char *
  3247. find_file (name)
  3248.      char *name;
  3249. {
  3250.   char *newname;
  3251.  
  3252.   newname = find_a_file (&startfile_prefix, name, R_OK);
  3253.   return newname ? newname : name;
  3254. }
  3255.  
  3256. /* Determine whether a -L option is relevant.  Not required for certain
  3257.    fixed names and for directories that don't exist.  */
  3258.  
  3259. static int
  3260. is_linker_dir (path1, path2)
  3261.      char *path1;
  3262.      char *path2;
  3263. {
  3264. #ifdef MPW
  3265.   return 0;
  3266. #else
  3267.   int len1 = strlen (path1);
  3268.   int len2 = strlen (path2);
  3269.   char *path = (char *) alloca (3 + len1 + len2);
  3270.   char *cp;
  3271.   struct stat st;
  3272.  
  3273.   /* Construct the path from the two parts.  Ensure the string ends with "/.".
  3274.      The resulting path will be a directory even if the given path is a
  3275.      symbolic link.  */
  3276.   bcopy (path1, path, len1);
  3277.   bcopy (path2, path + len1, len2);
  3278.   cp = path + len1 + len2;
  3279.   if (cp[-1] != '/')
  3280.     *cp++ = '/';
  3281.   *cp++ = '.';
  3282.   *cp = '\0';
  3283.  
  3284.   /* Exclude directories that the linker is known to search.  */
  3285.   if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
  3286.       || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
  3287.     return 0;
  3288.  
  3289.   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
  3290. #endif
  3291. }
  3292.  
  3293. /* On fatal signals, delete all the temporary files.  */
  3294.  
  3295. static void
  3296. fatal_error (signum)
  3297.      int signum;
  3298. {
  3299. #ifndef MPW
  3300.   signal (signum, SIG_DFL);
  3301. #endif
  3302.   delete_failure_queue ();
  3303.   delete_temp_files ();
  3304.   /* Get the same signal again, this time not handled,
  3305.      so its normal effect occurs.  */
  3306.   kill (getpid (), signum);
  3307. }
  3308.  
  3309. int
  3310. main (argc, argv)
  3311.      int argc;
  3312.      char **argv;
  3313. {
  3314.   register int i;
  3315.   int j;
  3316.   int value;
  3317.   int error_count = 0;
  3318.   int linker_was_run = 0;
  3319.   char *explicit_link_files;
  3320.   char *specs_file;
  3321.  
  3322.   programname = argv[0];
  3323.  
  3324. #ifndef MPW
  3325.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  3326.     signal (SIGINT, fatal_error);
  3327.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  3328.     signal (SIGHUP, fatal_error);
  3329.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  3330.     signal (SIGTERM, fatal_error);
  3331. #ifdef SIGPIPE
  3332.   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
  3333.     signal (SIGPIPE, fatal_error);
  3334. #endif
  3335. #endif
  3336.  
  3337.   argbuf_length = 10;
  3338.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  3339.  
  3340.   obstack_init (&obstack);
  3341.  
  3342.   /* Set up to remember the pathname of gcc and any options
  3343.      needed for collect.  */
  3344.   obstack_init (&collect_obstack);
  3345.   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
  3346.   obstack_grow (&collect_obstack, programname, strlen (programname)+1);
  3347.   putenv (obstack_finish (&collect_obstack));
  3348.  
  3349.   /* Choose directory for temp files.  */
  3350.  
  3351.   choose_temp_base ();
  3352.  
  3353.   /* Make a table of what switches there are (switches, n_switches).
  3354.      Make a table of specified input files (infiles, n_infiles).
  3355.      Decode switches that are handled locally.  */
  3356.  
  3357.   process_command (argc, argv);
  3358.  
  3359.   /* Initialize the vector of specs to just the default.
  3360.      This means one element containing 0s, as a terminator.  */
  3361.  
  3362.   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
  3363.   bcopy (default_compilers, compilers, sizeof default_compilers);
  3364.   n_compilers = n_default_compilers;
  3365.  
  3366.   /* Read specs from a file if there is one.  */
  3367.  
  3368.   machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
  3369.   just_machine_suffix = concat (spec_machine, "/", "");
  3370.  
  3371.   specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
  3372.   /* Read the specs file unless it is a default one.  */
  3373.   if (specs_file != 0 && strcmp (specs_file, "specs"))
  3374.     read_specs (specs_file);
  3375.  
  3376.   /* If not cross-compiling, look for startfiles in the standard places.  */
  3377.   /* The fact that these are done here, after reading the specs file,
  3378.      means that it cannot be found in these directories.
  3379.      But that's okay.  It should never be there anyway.  */
  3380.   if (!cross_compile)
  3381.     {
  3382. #ifdef MD_EXEC_PREFIX
  3383.       add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
  3384.       add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
  3385. #endif
  3386.  
  3387. #ifdef MD_STARTFILE_PREFIX
  3388.       add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
  3389. #endif
  3390.  
  3391. #ifdef MD_STARTFILE_PREFIX_1
  3392.       add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
  3393. #endif
  3394.  
  3395.       add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
  3396.           NULL_PTR);
  3397.       add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
  3398.           NULL_PTR);
  3399.       add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
  3400.           NULL_PTR);
  3401. #if 0 /* Can cause surprises, and one can use -B./ instead.  */
  3402.       add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
  3403. #endif
  3404.     }
  3405.  
  3406.   /* Now we have the specs.
  3407.      Set the `valid' bits for switches that match anything in any spec.  */
  3408.  
  3409.   validate_all_switches ();
  3410.  
  3411.   /* Warn about any switches that no pass was interested in.  */
  3412.  
  3413.   for (i = 0; i < n_switches; i++)
  3414.     if (! switches[i].valid)
  3415.       error ("unrecognized option `-%s'", switches[i].part1);
  3416.  
  3417.   if (print_libgcc_file_name)
  3418.     {
  3419.       printf ("%s\n", find_file ("libgcc.a"));
  3420.       exit (0);
  3421.     }
  3422.  
  3423.   /* Obey some of the options.  */
  3424.  
  3425.   if (verbose_flag)
  3426.     {
  3427.       fprintf (stderr, "gcc version %s\n", version_string);
  3428.       if (n_infiles == 0)
  3429.     exit (0);
  3430.     }
  3431.  
  3432.   if (n_infiles == 0)
  3433.     fatal ("No input files specified.");
  3434.  
  3435.   /* Make a place to record the compiler output file names
  3436.      that correspond to the input files.  */
  3437.  
  3438.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  3439.   bzero (outfiles, n_infiles * sizeof (char *));
  3440.  
  3441.   /* Record which files were specified explicitly as link input.  */
  3442.  
  3443.   explicit_link_files = xmalloc (n_infiles);
  3444.   bzero (explicit_link_files, n_infiles);
  3445.  
  3446.   for (i = 0; i < n_infiles; i++)
  3447.     {
  3448.       register struct compiler *cp = 0;
  3449.       int this_file_error = 0;
  3450.  
  3451.       /* Tell do_spec what to substitute for %i.  */
  3452.  
  3453.       input_filename = infiles[i].name;
  3454.       input_filename_length = strlen (input_filename);
  3455.       input_file_number = i;
  3456.  
  3457.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  3458.  
  3459.       outfiles[i] = input_filename;
  3460.  
  3461.       /* Figure out which compiler from the file's suffix.  */
  3462.  
  3463.       cp = lookup_compiler (infiles[i].name, input_filename_length,
  3464.                 infiles[i].language);
  3465.  
  3466.       if (cp)
  3467.     {
  3468.       /* Ok, we found an applicable compiler.  Run its spec.  */
  3469.       /* First say how much of input_filename to substitute for %b  */
  3470.       register char *p;
  3471.       int len;
  3472.  
  3473.       input_basename = input_filename;
  3474.       for (p = input_filename; *p; p++)
  3475.         if (*p == '/')
  3476.           input_basename = p + 1;
  3477.  
  3478.       /* Find a suffix starting with the last period,
  3479.          and set basename_length to exclude that suffix.  */
  3480.       basename_length = strlen (input_basename);
  3481.       p = input_basename + basename_length;
  3482.       while (p != input_basename && *p != '.') --p;
  3483.       if (*p == '.' && p != input_basename)
  3484.         {
  3485.           basename_length = p - input_basename;
  3486.           input_suffix = p + 1;
  3487.         }
  3488.       else
  3489.         input_suffix = "";
  3490.  
  3491.       len = 0;
  3492.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  3493.         if (cp->spec[j])
  3494.           len += strlen (cp->spec[j]);
  3495.  
  3496.       p = (char *) xmalloc (len + 1);
  3497.  
  3498.       len = 0;
  3499.       for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
  3500.         if (cp->spec[j])
  3501.           {
  3502.         strcpy (p + len, cp->spec[j]);
  3503.         len += strlen (cp->spec[j]);
  3504.           }
  3505.  
  3506.       value = do_spec (p);
  3507.       free (p);
  3508.       if (value < 0)
  3509.         this_file_error = 1;
  3510.     }
  3511.  
  3512.       /* If this file's name does not contain a recognized suffix,
  3513.      record it as explicit linker input.  */
  3514.  
  3515.       else
  3516.     explicit_link_files[i] = 1;
  3517.  
  3518.       /* Clear the delete-on-failure queue, deleting the files in it
  3519.      if this compilation failed.  */
  3520.  
  3521.       if (this_file_error)
  3522.     {
  3523.       delete_failure_queue ();
  3524.       error_count++;
  3525.     }
  3526.       /* If this compilation succeeded, don't delete those files later.  */
  3527.       clear_failure_queue ();
  3528.     }
  3529.  
  3530.   /* Run ld to link all the compiler output files.  */
  3531.  
  3532.   if (error_count == 0)
  3533.     {
  3534.       int tmp = execution_count;
  3535.       int i;
  3536.       int first_time;
  3537.  
  3538.       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
  3539.      for collect.  */
  3540.       putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
  3541.       putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
  3542.  
  3543.       /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
  3544.      the compiler.  */
  3545.       obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
  3546.             sizeof ("COLLECT_GCC_OPTIONS=")-1);
  3547.  
  3548.       first_time = TRUE;
  3549.       for (i = 0; i < n_switches; i++)
  3550.     {
  3551.       char **args;
  3552.       if (!first_time)
  3553.         obstack_grow (&collect_obstack, " ", 1);
  3554.  
  3555.       first_time = FALSE;
  3556.       obstack_grow (&collect_obstack, "-", 1);
  3557.       obstack_grow (&collect_obstack, switches[i].part1,
  3558.             strlen (switches[i].part1));
  3559.  
  3560.       for (args = switches[i].args; args && *args; args++)
  3561.         {
  3562.           obstack_grow (&collect_obstack, " ", 1);
  3563.           obstack_grow (&collect_obstack, *args, strlen (*args));
  3564.         }
  3565.     }
  3566.       obstack_grow (&collect_obstack, "\0", 1);
  3567.       putenv (obstack_finish (&collect_obstack));
  3568.  
  3569.       value = do_spec (link_command_spec);
  3570.       if (value < 0)
  3571.     error_count = 1;
  3572.       linker_was_run = (tmp != execution_count);
  3573.     }
  3574.  
  3575.   /* Warn if a -B option was specified but the prefix was never used.  */
  3576.   unused_prefix_warnings (&exec_prefix);
  3577.   unused_prefix_warnings (&startfile_prefix);
  3578.  
  3579.   /* If options said don't run linker,
  3580.      complain about input files to be given to the linker.  */
  3581.  
  3582.   if (! linker_was_run && error_count == 0)
  3583.     for (i = 0; i < n_infiles; i++)
  3584.       if (explicit_link_files[i])
  3585.     error ("%s: linker input file unused since linking not done",
  3586.            outfiles[i]);
  3587.  
  3588.   /* Delete some or all of the temporary files we made.  */
  3589.  
  3590.   if (error_count)
  3591.     delete_failure_queue ();
  3592.   delete_temp_files ();
  3593.  
  3594.   exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
  3595.   /* NOTREACHED */
  3596.   return 0;
  3597. }
  3598.  
  3599. /* Find the proper compilation spec for the file name NAME,
  3600.    whose length is LENGTH.  LANGUAGE is the specified language,
  3601.    or 0 if none specified.  */
  3602.  
  3603. static struct compiler *
  3604. lookup_compiler (name, length, language)
  3605.      char *name;
  3606.      int length;
  3607.      char *language;
  3608. {
  3609.   struct compiler *cp;
  3610.  
  3611.   /* Look for the language, if one is spec'd.  */
  3612.   if (language != 0)
  3613.     {
  3614.       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  3615.     {
  3616.       if (language != 0)
  3617.         {
  3618.           if (cp->suffix[0] == '@'
  3619.           && !strcmp (cp->suffix + 1, language))
  3620.         return cp;
  3621.         }
  3622.     }
  3623.       error ("language %s not recognized", language);
  3624.     }
  3625.  
  3626.   /* Look for a suffix.  */
  3627.   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
  3628.     {
  3629.       if (strlen (cp->suffix) < length
  3630.            /* See if the suffix matches the end of NAME.  */
  3631.            && !strcmp (cp->suffix,
  3632.                name + length - strlen (cp->suffix))
  3633.            /* The suffix `-' matches only the file name `-'.  */
  3634.            && !(!strcmp (cp->suffix, "-") && length != 1))
  3635.     {
  3636.       if (cp->spec[0][0] == '@')
  3637.         {
  3638.           struct compiler *new;
  3639.           /* An alias entry maps a suffix to a language.
  3640.          Search for the language; pass 0 for NAME and LENGTH
  3641.          to avoid infinite recursion if language not found.
  3642.          Construct the new compiler spec.  */
  3643.           language = cp->spec[0] + 1;
  3644.           new = (struct compiler *) xmalloc (sizeof (struct compiler));
  3645.           new->suffix = cp->suffix;
  3646.           bcopy (lookup_compiler (NULL_PTR, 0, language)->spec,
  3647.              new->spec, sizeof new->spec);
  3648.           return new;
  3649.         }
  3650.       /* A non-alias entry: return it.  */
  3651.       return cp;
  3652.     }
  3653.     }
  3654.  
  3655.   return 0;
  3656. }
  3657.  
  3658. char *
  3659. xmalloc (size)
  3660.      unsigned size;
  3661. {
  3662.   register char *value = (char *) malloc (size);
  3663.   if (value == 0)
  3664.     fatal ("virtual memory exhausted");
  3665.   return value;
  3666. }
  3667.  
  3668. char *
  3669. xrealloc (ptr, size)
  3670.      char *ptr;
  3671.      unsigned size;
  3672. {
  3673.   register char *value = (char *) realloc (ptr, size);
  3674.   if (value == 0)
  3675.     fatal ("virtual memory exhausted");
  3676.   return value;
  3677. }
  3678.  
  3679. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  3680.  
  3681. static char *
  3682. concat (s1, s2, s3)
  3683.      char *s1, *s2, *s3;
  3684. {
  3685.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  3686.   char *result = xmalloc (len1 + len2 + len3 + 1);
  3687.  
  3688.   strcpy (result, s1);
  3689.   strcpy (result + len1, s2);
  3690.   strcpy (result + len1 + len2, s3);
  3691.   *(result + len1 + len2 + len3) = 0;
  3692.  
  3693.   return result;
  3694. }
  3695.  
  3696. static char *
  3697. save_string (s, len)
  3698.      char *s;
  3699.      int len;
  3700. {
  3701.   register char *result = xmalloc (len + 1);
  3702.  
  3703.   bcopy (s, result, len);
  3704.   result[len] = 0;
  3705.   return result;
  3706. }
  3707.  
  3708. static void
  3709. pfatal_with_name (name)
  3710.      char *name;
  3711. {
  3712.   char *s;
  3713.  
  3714. #ifndef MPW
  3715.   if (errno < sys_nerr)
  3716.     s = concat ("%s: ", sys_errlist[errno], "");
  3717.   else
  3718. #endif
  3719.     s = "cannot open %s";
  3720.   fatal (s, name);
  3721. }
  3722.  
  3723. static void
  3724. perror_with_name (name)
  3725.      char *name;
  3726. {
  3727.   char *s;
  3728.  
  3729. #ifndef MPW
  3730.   if (errno < sys_nerr)
  3731.     s = concat ("%s: ", sys_errlist[errno], "");
  3732.   else
  3733. #endif
  3734.     s = "cannot open %s";
  3735.   error (s, name);
  3736. }
  3737.  
  3738. static void
  3739. perror_exec (name)
  3740.      char *name;
  3741. {
  3742.   char *s;
  3743.  
  3744. #ifndef MPW
  3745.   if (errno < sys_nerr)
  3746.     s = concat ("installation problem, cannot exec %s: ",
  3747.         sys_errlist[errno], "");
  3748.   else
  3749. #endif
  3750.     s = "installation problem, cannot exec %s";
  3751.   error (s, name);
  3752. }
  3753.  
  3754. /* More 'friendly' abort that prints the line and file.
  3755.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  3756.  
  3757. void
  3758. fancy_abort ()
  3759. {
  3760.   fatal ("Internal gcc abort.");
  3761. }
  3762.  
  3763. #ifdef HAVE_VPRINTF
  3764.  
  3765. /* Output an error message and exit */
  3766.  
  3767. static void
  3768. fatal (va_alist)
  3769.      va_dcl
  3770. {
  3771.   va_list ap;
  3772.   char *format;
  3773.  
  3774.   va_start (ap);
  3775.   format = va_arg (ap, char *);
  3776.   fprintf (stderr, "%s: ", programname);
  3777.   vfprintf (stderr, format, ap);
  3778.   va_end (ap);
  3779.   fprintf (stderr, "\n");
  3780.   delete_temp_files ();
  3781.   exit (1);
  3782. }
  3783.  
  3784. static void
  3785. error (va_alist)
  3786.      va_dcl
  3787. {
  3788.   va_list ap;
  3789.   char *format;
  3790.  
  3791.   va_start (ap);
  3792.   format = va_arg (ap, char *);
  3793.   fprintf (stderr, "%s: ", programname);
  3794.   vfprintf (stderr, format, ap);
  3795.   va_end (ap);
  3796.  
  3797.   fprintf (stderr, "\n");
  3798. }
  3799.  
  3800. #else /* not HAVE_VPRINTF */
  3801.  
  3802. static void
  3803. fatal (msg, arg1, arg2)
  3804.      char *msg, *arg1, *arg2;
  3805. {
  3806.   error (msg, arg1, arg2);
  3807.   delete_temp_files ();
  3808.   exit (1);
  3809. }
  3810.  
  3811. static void
  3812. error (msg, arg1, arg2)
  3813.      char *msg, *arg1, *arg2;
  3814. {
  3815.   fprintf (stderr, "%s: ", programname);
  3816.   fprintf (stderr, msg, arg1, arg2);
  3817.   fprintf (stderr, "\n");
  3818. }
  3819.  
  3820. #endif /* not HAVE_VPRINTF */
  3821.  
  3822.  
  3823. static void
  3824. validate_all_switches ()
  3825. {
  3826.   struct compiler *complr;
  3827.   register char *p;
  3828.   register char c;
  3829.   struct spec_list *spec;
  3830.  
  3831.   for (complr = compilers; complr->spec[0]; complr++)
  3832.     {
  3833.       int i;
  3834.       for (i = 0; i < sizeof complr->spec / sizeof complr->spec[0] && complr->spec[i]; i++)
  3835.     {
  3836.       p = complr->spec[i];
  3837.       while (c = *p++)
  3838.         if (c == '%' && *p == '{')
  3839.           /* We have a switch spec.  */
  3840.           validate_switches (p + 1);
  3841.     }
  3842.     }
  3843.  
  3844.   /* look through the linked list of extra specs read from the specs file */
  3845.   for (spec = specs; spec ; spec = spec->next)
  3846.     {
  3847.       p = spec->spec;
  3848.       while (c = *p++)
  3849.     if (c == '%' && *p == '{')
  3850.       /* We have a switch spec.  */
  3851.       validate_switches (p + 1);
  3852.     }
  3853.  
  3854.   p = link_command_spec;
  3855.   while (c = *p++)
  3856.     if (c == '%' && *p == '{')
  3857.       /* We have a switch spec.  */
  3858.       validate_switches (p + 1);
  3859.  
  3860.   /* Now notice switches mentioned in the machine-specific specs.  */
  3861.  
  3862.   p = asm_spec;
  3863.   while (c = *p++)
  3864.     if (c == '%' && *p == '{')
  3865.       /* We have a switch spec.  */
  3866.       validate_switches (p + 1);
  3867.  
  3868.   p = asm_final_spec;
  3869.   while (c = *p++)
  3870.     if (c == '%' && *p == '{')
  3871.       /* We have a switch spec.  */
  3872.       validate_switches (p + 1);
  3873.  
  3874.   p = cpp_spec;
  3875.   while (c = *p++)
  3876.     if (c == '%' && *p == '{')
  3877.       /* We have a switch spec.  */
  3878.       validate_switches (p + 1);
  3879.  
  3880.   p = signed_char_spec;
  3881.   while (c = *p++)
  3882.     if (c == '%' && *p == '{')
  3883.       /* We have a switch spec.  */
  3884.       validate_switches (p + 1);
  3885.  
  3886.   p = cc1_spec;
  3887.   while (c = *p++)
  3888.     if (c == '%' && *p == '{')
  3889.       /* We have a switch spec.  */
  3890.       validate_switches (p + 1);
  3891.  
  3892.   p = cc1plus_spec;
  3893.   while (c = *p++)
  3894.     if (c == '%' && *p == '{')
  3895.       /* We have a switch spec.  */
  3896.       validate_switches (p + 1);
  3897.  
  3898.   p = link_spec;
  3899.   while (c = *p++)
  3900.     if (c == '%' && *p == '{')
  3901.       /* We have a switch spec.  */
  3902.       validate_switches (p + 1);
  3903.  
  3904.   p = lib_spec;
  3905.   while (c = *p++)
  3906.     if (c == '%' && *p == '{')
  3907.       /* We have a switch spec.  */
  3908.       validate_switches (p + 1);
  3909.  
  3910.   p = startfile_spec;
  3911.   while (c = *p++)
  3912.     if (c == '%' && *p == '{')
  3913.       /* We have a switch spec.  */
  3914.       validate_switches (p + 1);
  3915. }
  3916.  
  3917. /* Look at the switch-name that comes after START
  3918.    and mark as valid all supplied switches that match it.  */
  3919.  
  3920. static void
  3921. validate_switches (start)
  3922.      char *start;
  3923. {
  3924.   register char *p = start;
  3925.   char *filter;
  3926.   register int i;
  3927.   int suffix = 0;
  3928.  
  3929.   if (*p == '|')
  3930.     ++p;
  3931.  
  3932.   if (*p == '!')
  3933.     ++p;
  3934.  
  3935.   if (*p == '.')
  3936.     suffix = 1, ++p;
  3937.  
  3938.   filter = p;
  3939.   while (*p != ':' && *p != '}') p++;
  3940.  
  3941.   if (suffix)
  3942.     ;
  3943.   else if (p[-1] == '*')
  3944.     {
  3945.       /* Mark all matching switches as valid.  */
  3946.       --p;
  3947.       for (i = 0; i < n_switches; i++)
  3948.     if (!strncmp (switches[i].part1, filter, p - filter))
  3949.       switches[i].valid = 1;
  3950.     }
  3951.   else
  3952.     {
  3953.       /* Mark an exact matching switch as valid.  */
  3954.       for (i = 0; i < n_switches; i++)
  3955.     {
  3956.       if (!strncmp (switches[i].part1, filter, p - filter)
  3957.           && switches[i].part1[p - filter] == 0)
  3958.         switches[i].valid = 1;
  3959.     }
  3960.     }
  3961. }
  3962.  
  3963. #ifdef MPW
  3964.  
  3965. wait (int *statloc) { *statloc = 0; return 1; }
  3966.  
  3967. sleep () {}
  3968.  
  3969. vfork () {}
  3970.  
  3971. pipe () {}
  3972.  
  3973. #endif
  3974.